[英]Array if contains value, unset
I got follow array ($json_output): 我得到了关注数组($ json_output):
array(3) {
["ProductsSummary"]=>
array(4) {
["Records"]=>
int(500)
["TotalRecords"]=>
int(5720)
["TotalPages"]=>
int(12)
["CurrentPage"]=>
int(2)
}
["Products"]=>
array(500) {
[0]=>
array(10) {
["ProductId"]=>
int(1323819499)
["ShopId"]=>
int(1856)
["ProductName"]=>
string(21) "Fossil Creole JF84757"
["Deeplink2"]=>
string(0) ""
["Brand"]=>
NULL
["Manufacturer"]=>
string(6) "Fossil"
["Distributor"]=>
NULL
["EAN"]=>
string(13) "4048803717479"
["Keywords"]=>
NULL
["Properties"]=>
array(3) {
[0]=>
array(2) {
["PropertyName"]=>
string(12) "DeliveryTime"
["PropertyValue"]=>
string(1) "5"
}
[1]=>
array(2) {
["PropertyName"]=>
string(17) "MerchantArtNumber"
["PropertyValue"]=>
string(8) "85145452"
}
[2]=>
array(2) {
["PropertyName"]=>
string(6) "gender"
["PropertyValue"]=>
string(5) "Damen"
}
}
}
[1]=>
array(10) {
["ProductId"]=>
int(1323819505)
["ShopId"]=>
int(1856)
["ProductName"]=>
string(16) "SANSIBAR Armband"
["Deeplink2"]=>
string(0) ""
["Brand"]=>
NULL
["Manufacturer"]=>
string(8) "Sansibar"
["Distributor"]=>
NULL
["EAN"]=>
NULL
["Keywords"]=>
NULL
["Properties"]=>
array(3) {
[0]=>
array(2) {
["PropertyName"]=>
string(12) "DeliveryTime"
["PropertyValue"]=>
string(1) "5"
}
[1]=>
array(2) {
["PropertyName"]=>
string(17) "MerchantArtNumber"
["PropertyValue"]=>
string(8) "85189719"
}
[2]=>
array(2) {
["PropertyName"]=>
string(6) "gender"
["PropertyValue"]=>
string(5) "Herren"
}
}
}
I need to unset all Products which contains 'Herren' in Properties, so I tried: 我需要取消设置所有在属性中包含“ Herren”的产品,因此我尝试:
<?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);
}
?>
But it's not working. 但这不起作用。
array_filter iterates for you and returns a filtered set of elements. array_filter为您进行迭代,并返回一组过滤后的元素。
The callback function returns true if the element is to remain, and false if it is to be removed. 如果要保留元素,则回调函数返回true;如果要删除该元素,则返回false。 json_encode converts the whole array into a string, strpos looks for the string Herren anywhere in that string.
json_encode将整个数组转换为字符串,strpos在该字符串中的任何位置查找字符串Herren。 Since you require no regular expression, there's no need to use preg_match which is slower than strpos.
由于不需要正则表达式,因此无需使用比strpos慢的preg_match。
$array['Products']=array_filter($array['Products'], 'removeHerren');
function removeHerren($array){
return strpos(json_encode($array), 'Herren')===false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.