P
P
Pavel2020-04-30 14:32:20
PHP
Pavel, 2020-04-30 14:32:20

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"
  }

and
$result_days:
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"
  }


I am making a table in thead in which it is necessary to make a selection for the days that match in both arrays,
I try to do this
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>";
                }
            }
        }

$dateUsers['off_date'] === $i2['date'] - doesn't work and all days go to else , why is that?
The thing is that you need to save all the days in ascending order

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2020-04-30
@mrusklon

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 question

Ask a Question

731 491 924 answers to any question