Php 数组如果包含值,则取消设置
我得到了以下数组($json\u输出): 我需要取消设置属性中包含“Herren”的所有产品,因此我尝试:Php 数组如果包含值,则取消设置,php,arrays,unset,Php,Arrays,Unset,我得到了以下数组($json\u输出): 我需要取消设置属性中包含“Herren”的所有产品,因此我尝试: <?php foreach($json_output["Products"] as & $bla) $check = $bla["Properties"][0]["PropertyValue"] . $bla["Properties"][1]["PropertyValue"] . $bla["Properties"][2]["PropertyValue"]; if (preg
<?php
foreach($json_output["Products"] as & $bla)
$check = $bla["Properties"][0]["PropertyValue"] . $bla["Properties"][1]["PropertyValue"] . $bla["Properties"][2]["PropertyValue"];
if (preg_match('/Herren/',$check))
{
unset($bla);
}
?>
但它不起作用。为您迭代并返回一组经过筛选的元素
如果要保留元素,回调函数将返回true;如果要删除元素,回调函数将返回false。json_encode将整个数组转换为字符串,strpos在该字符串中的任何位置查找字符串Herren。因为您不需要正则表达式,所以不需要使用比strpos慢的preg_match
$array['Products']=array_filter($array['Products'], 'removeHerren');
function removeHerren($array){
return strpos(json_encode($array), 'Herren')===false;
}
在循环过程中,如果发现“Herren”,只需跳过循环“continue;”或者将产品附加到新阵列并使用其kip继续循环?但我需要它来继续这个阵列包含数千个产品。创建一个新的数组,在属性中没有“Herren”,这就是我要做的
$array['Products']=array_filter($array['Products'], 'removeHerren');
function removeHerren($array){
return strpos(json_encode($array), 'Herren')===false;
}