2017-05-29 84 views
-2

我有兩個數組:如何回聲多維數組的比較在PHP中

$one = Array 
(
    [0] => stdClass Object 
     (
      [id] => 2 
      [name] => Southampton 
     ) 

    [1] => stdClass Object 
     (
      [id] => 4 
      [name] => Manchester United F.C 
     ) 

) 

$two = Array 
(
    [0] => stdClass Object 
     (
      [number] => 25555 
      [slice_1] => 4 
      [slice_2] => 4 
      [slice_3] => 2 
      [slice_4] => 4 
      [status] => Published 
     ) 

) 

我想輸出,如果陣列$two->slice_1相同陣列$one->id比輸出$one->name

例如:

$two[0]->slice_1(4)比較$one[0]->id(4)將導致曼聯F.C.因爲數組$one$two將會有多個數組。請不要回答這個問題:

if($two[0]->slice_1 == $one[1]->id){echo $one[0]->name;} 

我想在這裏,想不出什麼辦法。請幫忙。在此先感謝

+0

你有什麼你至今tryed? –

+0

我試過了: '$ count = count($ two); 爲($ i = 0; $ I <= $計數; $ I ++){ \t \t \t \t \t \t如果($ 2個[$ i]於 - > slice_1 == $酮[$ i]於 - > id)的{ \t \t \t \t echo $ two [$ i] - > slice_1。' :'。$ one [$ i] - > name; \t \t \t \t \t \t \t \t}' –

回答

0

避免循環中的第一循環創建臨時引用數組然後循環,雖然第二陣列輸出結果

foreach($one as $id=>$data){ 
    $temp[$data['id']] = $data['name']; 
} 
foreach($two as $secondary_id=>$secondary_data){ 
    echo isset($temp[$secondary_data['slice_1']]) ? $temp[$secondary_data['slice_1']] : ''; 
}