2015-04-08 60 views
1

我有一個多維陣列:PHP未設置多維數組元素哪個密鑰值是匹配

Array 
(
    [0] => stdClass Object 
     (
      [user_type] => U 
      [user_id] => 156 
      [property_id] => 1201 
     ) 

    [1] => stdClass Object 
     (
      [user_type] => U 
      [user_id] => 156 
      [property_id] => 1200 
     ) 

    [2] => stdClass Object 
     (
      [user_type] => U 
      [user_id] => 156 
      [property_id] => 1196 
     ) 

    [3] => stdClass Object 
     (
      [user_type] => U 
      [user_id] => 156 
      [property_id] => 1193 
     ) 
) 

和我想刪除陣列值是匹配與property_id

Array 
(
    [0] => 1201 
    [1] => 1200 
    [2] => 1193 
) 

,我想這結果:

Array 
(
    [0] => stdClass Object 
     (
      [user_type] => U 
      [user_id] => 156 
      [property_id] => 1196 
     ) 
) 

我與我分享我的代碼:

for($b=0; $b<count($beBounceResults); $b++){ 
    $beBounceProID[] = $beBounceResults[$b]->property_id; 
} 
// Getting thus array in this variable $beBounceProID 
Array 
(
    [0] => 1201 
    [1] => 1200 
    [2] => 1193 
) 

$counter = "0"; 
foreach ($results as $key => $value){ 
    if($results[$key]->property_id == $beBounceProID[$counter]){ 
     unset($results[$key]); 
    } 
    $counter++; 
} 

但在那之後我收到Notice: Undefined offset:

任何想法,我做錯了。

謝謝。

+0

而不是$反= 「0」;使用$ counter = 0; –

+0

您在列表中有1193個,但仍想保留它? – Rizier123

+0

@anantkumarsingh這沒有區別!絕對錯誤,如果它的索引爲0,它會將它隱含到int – Rizier123

回答

4

試試這個

foreach ($results as $key => $value){ 
    if(in_array($results[$key]->property_id , $beBounceProID)) 
    { 
     unset($results[$key]); 
    } 
} 
+1

我認爲你的回答是相當正確的xD +1 – vivoconunxino

+0

快速且簡單易懂+1 – Duenna

1

另一種方法是隻過濾你不想出這樣的陣列:

$arr = array_filter($arr, function($v)use($property_id){ 
    return !in_array($v->property_id, $property_id); 
}); 
相關問題