2016-04-20 2 views
0

ここは私の配列です。多次元配列unset not working

var_dump($contact['poco']['tags']); 

array (size=5) 
    0 => 
    array (size=3) 
     'tag' => string 'boy' (length=3) 
     'color' => string '#332409' (length=7) 
     'id' => string '57160583b0e6df19598b4568' (length=24) 
    1 => 
    array (size=3) 
     'tag' => string 'girl' (length=4) 
     'color' => string '#2e2f15' (length=7) 
     'id' => string '57160589b0e6df1d598b4567' (length=24) 
    2 => 
    array (size=3) 
     'tag' => string 'zebra' (length=5) 
     'color' => string '#646604' (length=7) 
     'id' => string '57160592b0e6df7b588b4567' (length=24) 
    3 => 
    array (size=3) 
     'tag' => string 'potential duplicate' (length=19) 
     'color' => string '#f00' (length=4) 
     'id' => string '57161d9db0e6df0f5c8b456b' (length=24) 
    4 => 
    array (size=3) 
     'tag' => string 'no phone numbers' (length=16) 
     'color' => string '#5833d2' (length=7) 
     'id' => string '5716059ab0e6df7b588b456d' (length=24) 

次のタグを持つものをアンセット/削除したいだけです。

$smartTags = ['potential duplicate', 'no emails', 'no phone numbers']; 

array (size=3) 
    0 => 
    array (size=3) 
     'tag' => string 'boy' (length=3) 
     'color' => string '#332409' (length=7) 
     'id' => string '57160583b0e6df19598b4568' (length=24) 
    1 => 
    array (size=3) 
     'tag' => string 'girl' (length=4) 
     'color' => string '#2e2f15' (length=7) 
     'id' => string '57160589b0e6df1d598b4567' (length=24) 
    2 => 
    array (size=3) 
     'tag' => string 'zebra' (length=5) 
     'color' => string '#646604' (length=7) 
     'id' => string '57160592b0e6df7b588b4567' (length=24) 

私は試しました。

$smartTags = ['potential duplicate', 'no emails', 'no phone numbers']; 

foreach ($contact['poco']['tags'] as $key => $tag) { 

    if (in_array($tag, $smartTags)) { 
     unset($contact['poco']['tags'][$key]); 
    } 
} 

しかし、何もしません。この配列の多次元性のために問題が発生する可能性があります。

正しい構文は何ですか?

+1

'場合(in_array($タグ[ 'タグ']、$スマートタグ)){' – splash58

+0

あなたの状態ではない本当の.... –

+0

splash58 @ - ありがとう。できます。時にはほんの2番目の目が助けになる – mikelovelyuk

答えて

0

これを試してください。

foreach ($contact['poco']['tags'] as $key => $tag) {   
    if (in_array($tag['tag'], $smartTags)) { 
     unset($contact['poco']['tags'][$key]); 
    }  
}