多维数组未设置无效

时间:2016-04-20 08:54:02

标签: php arrays multidimensional-array

这是我的阵列;

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 个答案:

答案 0 :(得分:0)

试试这个。

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