【问题标题】:remove subarray with php用php删除子数组
【发布时间】:2012-06-05 19:45:14
【问题描述】:

我有以下功能,其目的是在数组树中过滤那些不符合搜索索引的元素并消除主题。我可以得到这个函数来带来想要的结果。

public function negativeKeywordsFilter($products, $negative_keywords){
  $nk=explode(',',$negative_keywords);
  foreach ($products['productItems'] as $product){
    foreach ($product as $item){
        foreach ($nk as $word){
        if (stripos($item['name'],$word) !== false){
        unset($item);                       
    }

  }
}

}
 return $products;
}

我的数组如下所示:

array(
    'page' => '1',
    'items' => '234',
    'items' => array(
        'item' => array(
            0 => array(
                'name' => 'second', 
                'description' => 'some description'
            )
        )
    )
)
)

如果名称与描述匹配,则应取消设置值。

【问题讨论】:

  • 您能否提供预期输入和输出的示例?
  • @lgt 真的 你的数组看起来像吗?重复键?真的吗?
  • 我要把我的输出放在这里
  • 你知道我每天都在学习新事物

标签: php arrays unset


【解决方案1】:

问题是你只取消了一个具有 copy 值的变量,你需要取消设置数组中的相应元素。

public function negativeKeywordsFilter($products, $negative_keywords){
  $nk=explode(',',$negative_keywords);
  foreach ($products['productItems'] as $key1 => $product){
    foreach ($product as $key2 => $item){
        foreach ($nk as $word){
        if (stripos($item['name'],$word) !== false){
        unset($products['productItems'][$key1][$key2]);                       
    }

  }
}

}
 return $products;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    相关资源
    最近更新 更多