I’m trying to compare two PHP arrays with different information.
First array:
Array
(
[B1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
)
[B2] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
[B3] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
)
[B4] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
)
)
Second array:
Array
(
[1] => B4
[2] => B3
[3] => B1
)
Now I want this result:
Array
(
[B1] => Array
(
[0] => ok
[1] => ok
[2] => ok
[3] => ok
[4] => ok
[5] => ok
[6] => ok
[7] => ok
)
[B2] => Array
(
[0] => null
[1] => null
[2] => null
[3] => null
)
[B3] => Array
(
[0] => ok
[1] => ok
[2] => ok
[3] => ok
[4] => ok
[5] => ok
[6] => ok
[7] => ok
)
[B4] => Array
(
[0] => ok
[1] => ok
[2] => ok
[3] => ok
[4] => ok
[5] => ok
[6] => ok
[7] => ok
[8] => ok
)
)
I’ve tried various PHP functions, but they didn’t return expected result.
I’ve tried various PHP functions such as in_array
and stristr
to get the differences, but they returns different results and empty arrays.
Hope someone can help me, and thanks.
4
Answers
Solution:
The result is:
Definition:
This solution stores the result inside the first array, that means data from the first array will be lost. If you want to keep the data you can check this answer to copy data to another array:
https://stackoverflow.com/a/17729234/10109156
Or, you can create a new array to store the result:
And result is same.
trying with this code