2017-10-21 169 views
0

我有兩個多維數組,比較兩個多維數組

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

和:

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

我需要比較$textEvaluasi$hasil,如果key和value匹配的話,我會打印鍵和價值。

我試過了,但它不能正常工作。下面是我做的那一刻:

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

而且我的代碼的輸出是:

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

預期輸出:

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

任何幫助非常感謝,謝謝。

回答

2

您可以使用array_filter爲如下:

$result = array_filter($textEvaluasi, function ($arr, $key) use ($hasil) { 
    return $hasil[$key] === $arr; 
}, ARRAY_FILTER_USE_BOTH); 
+0

感謝知識,,, – GYaN