Answer the question
In order to leave comments, you need to log in
How to compare 2 multidimensional arrays and keep order?
There are 2 arrays
$result2:
array(29) {
[0]=>
array(5) {
["users_id"]=>
string(2) "19"
["date"]=>
string(10) "2020-04-01"
["user_id"]=>
string(2) "19"
["sub_hours"]=>
string(1) "8"
}
[71]=>
array(5) {
["users_id"]=>
string(3) "108"
["date"]=>
string(10) "2020-04-02"
["user_id"]=>
string(3) "108"
["sub_hours"]=>
string(1) "8"
}
[139]=>
array(5) {
["users_id"]=>
string(2) "91"
["date"]=>
string(10) "2020-04-03"
["user_id"]=>
string(2) "91"
["sub_hours"]=>
string(1) "8"
}
array(202) {
[0]=>
array(3) {
["id"]=>
string(3) "123"
["off_date"]=>
string(10) "2019-04-27"
["off_id"]=>
string(1) "1"
}
[1]=>
array(3) {
["id"]=>
string(3) "118"
["off_date"]=>
string(10) "2019-04-28"
["off_id"]=>
string(1) "0"
}
[2]=>
array(3) {
["id"]=>
string(3) "111"
["off_date"]=>
string(10) "2019-04-29"
["off_id"]=>
string(1) "0"
}
[3]=>
array(3) {
["id"]=>
string(3) "112"
["off_date"]=>
string(10) "2019-04-30"
["off_id"]=>
string(1) "0"
}
[4]=>
array(3) {
["id"]=>
string(3) "119"
["off_date"]=>
string(10) "2019-05-01"
["off_id"]=>
string(1) "0"
}
[5]=>
array(3) {
["id"]=>
string(3) "120"
["off_date"]=>
string(10) "2019-05-05"
["off_id"]=>
string(1) "0"
}
foreach ($result_days as $dateUsers) {
foreach ($result2 as $i2) {
if ($dateUsers['off_date'] === $i2['date']) {
echo "<td class='vihodnoy'>" . $i2['date'] . "</td>";
} else {
echo "<td class='obichniyDen'>" . $i2['date'] . "</td>";
}
}
}
Answer the question
In order to leave comments, you need to log in
It is not at all clear from the code what is being compared with what. If the arrays are in the $result_days and $result2 variables, then a cell will be displayed for each pair of elements (i.e. the total number of cells will be equal to the product of the array sizes). You probably didn't think about it.
On the main question: looking for something in a multidimensional array without indexes is unpleasant. Make the keys in the array you are comparing against, like so:
$result_days = [
"2019-04-27" => [ "id" => "123", ... ],
...
];
foreach ($result2 as $i2) {
if (array_key_exists($i2['date'], $result_days) {
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question