2014-09-05 66 views
0

我有這樣的多維數組:在多維數組與匹配PHP刪除元素

[0] => Array 
    (
     [id] => 55829 
     [date] => 2014-09-05 07:00:56 
     [customer] => Engineering 
     [server] => example 
     [status] => Successful 
     [version] => 1 
    ) 

[1] => Array 
    (
     [id] => 55776 
     [date] => 2014-09-05 06:58:30 
     [customer] => Coating 
     [server] => example 
     [status] => Successful 
     [version] => 1 
    ) 

我想通過數組,如果第二級「客戶」值這個陣列中的值相匹配才能夠循環:

Array 
(
    [0] => Engineering 
    [1] => Painting 
) 

然後我要完全刪除/取消設置父陣列,如果有一個匹配,使得第一陣列然後將:

[0] => Array 
    (
     [id] => 55776 
     [date] => 2014-09-05 06:58:30 
     [customer] => Coating 
     [server] => example 
     [status] => Successful 
     [version] => 1 
    ) 

回答

2

以下應該在$records是多維數組和$second_array包含工程/繪畫。

<?php 
foreach($records as $key => $record) 
{ 
    if(in_array($record['customer'], $second_array)) 
    { 
     unset($records[ $key ]); 
    } 
} 
?> 
+0

拍攝,您可以通過12秒打我。有一個upvote。 ;) – bzeaman 2014-09-05 10:35:01

+0

非常感謝喲!作品一種享受! :) – MikeeeGeee 2014-09-05 10:40:57

0

使用in_array

$array_to_filter = array(array(...)); 
$array_filter = array(...); 
foreach ($array_to_filter as $key => $value) { 
    if (in_array($value['customer'], $array_filter)) { 
     unset($array_to_filter['$key']); 
    } 
} 
0

嘗試:

$types = array('Engineering', 'Painting'); 

foreach($array as $key => $v) { 
    if(in_array($v['customer'], $types)) { 
     unset($array[$key]); 
    } 
} 
0

你可以嘗試:

$arrayBlackList = array('Engineering','Painting'); 

function shouldKeep($var) 
{ 
    global $arrayBlackList; 
    return !in_array($var['customer'],$arrayBlackList); 
} 

$filtered_array = array_filter($array1, "shouldKeep"); 
0
$i = 0; 
for ($i = 0; $i <count($array1) ; $i++) { 

    if ($array1[$i]['customer'] == $array2[0] || $array1[$i]['customer'] == $array2[1]) { 

     unset($array[$i]); 

     // restart 
     $i = 0 
    } 
}