簡體   English   中英

多維數組未設置不起作用

[英]multidimensional array 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]);
    }
}

但是它什么也沒做。 由於此數組的多維性,我可能會遇到麻煩...

正確的語法是什么?

試試這個。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM