0

I have two multi-dimensional arrays,

$textEvaluasi = [
                [0]=>[
                      [0]     => "BTC Hotel",
                      [1]     => "Hotel Jaya"
                     ]
                     ,
                [3]=>[
                      [0]     => "BTC Hotel"
                     ]
                     ,
                [4]=>[
                      [0]     => "Lokasi BTC Hotel"
                     ]
                     ,
                [5]=>[
                      [0]     => "Lokasi BTC Hotel"
                     ]
                ];

And :

$hasil =                  [
                            [0]=>[
                                  [0]     => "BTC Hotel",
                                  [1]     => "Hotel Jaya"
                                 ]
                                 ,
                            [3]=>[
                                  [0]     => "BTC Hotel"
                                 ]
                                 ,
                            [4]=>[
                                  [0]     => "BTC Hotel"
                                 ]
                                 ,
                            [5]=>[
                                  [0]     => "BTC Hotel"
                                 ]
                            ];

I need to compare $textEvaluasi and $hasil, if key and value is matched then I will print key and value.

I've tried but it's not working properly. Here's what I am doing for the moment:

foreach ($textEvaluasi as $key => $value) {
  foreach ($value as $key2 => $value2) {
      foreach ($hasil as $key3 => $value3) {
        foreach ($value3 as $key4 => $value4) {
          if (strtolower($textEvaluasi[$key][$key2]) === strtolower($hasil[$key3][$key4])){
            echo $key . $value2; 
          }
        }
      }
     echo "<br>"; 
  }
}

And the output of my code is:

 [0] -> BTC Hotel
 [3] -> BTC Hotel
 [4] -> BTC Hotel
 [5] -> BTC Hotel

Expected output:

[0] -> BTC Hotel, Hotel Jaya
[3] -> BTC Hotel

Any help is much appreciated, Thank you.

1 Answers1

2

You can use array_filter for that, as follows:

$result = array_filter($textEvaluasi, function ($arr, $key) use ($hasil) {
    return $hasil[$key] === $arr;
}, ARRAY_FILTER_USE_BOTH);
trincot
  • 317,000
  • 35
  • 244
  • 286