简体   繁体   中英

Array if contains value, unset

I got follow array ($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:

<?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.

The callback function returns true if the element is to remain, and false if it is to be removed. json_encode converts the whole array into a string, strpos looks for the string Herren anywhere in that string. Since you require no regular expression, there's no need to use preg_match which is slower than strpos.

$array['Products']=array_filter($array['Products'], 'removeHerren');

function removeHerren($array){
    return strpos(json_encode($array), 'Herren')===false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM