从php中的对象数组中删除对象

问题描述 投票:0回答:1

我有一个从Web服务返回的对象(产品)数组,看起来像这样:

array (size=2)
  'productInventory' => 
    array (size=2)
      0 => 
        object(stdClass)[127]
          public 'product_id' => string '267254' (length=6)
          public 'product_name' => string 'Confiteria Doble Cine' (length=21)
          public 'product_sku' => string 'MISIKTFN71P' (length=11)
          public 'category_name' => string 'Cines
' (length=7)
          public 'caT' => string 'BONOS DIGITALES' (length=15)
          public 'provider_id' => string '280' (length=3)
          public 'provider_bussines_name' => string 'Quantum Rewards Coltabaco' (length=25)
          public 'TYPE' => string '5' (length=1)
          public 'ISMONEY' => string '0' (length=1)
          public 'priceM' => string '1014' (length=4)
          public 'priceP' => string '1014' (length=4)
          public 'STOCK' => string '14000' (length=5)
          public 'creationDate' => string '2019-09-05 17:27:16' (length=19)
          public 'FOTO' => string 'http://cache.megastore.com.co/MISIKTFN71P_280_20190905172720_5d718bc8bb00a.jpg' (length=78)
      1 => 
        object(stdClass)[111]
          public 'product_id' => string '267235' (length=6)
          public 'product_name' => string 'Entrada a Cine Doble 3D' (length=23)
          public 'product_sku' => string 'MIXFAGOWQL4' (length=11)
          public 'category_name' => string 'Cines
' (length=7)
          public 'caT' => string 'BONOS DIGITALES' (length=15)
          public 'provider_id' => string '280' (length=3)
          public 'provider_bussines_name' => string 'Quantum Rewards Coltabaco' (length=25)
          public 'TYPE' => string '5' (length=1)
          public 'ISMONEY' => string '0' (length=1)
          public 'priceM' => string '1665' (length=4)
          public 'priceP' => string '1665' (length=4)
          public 'STOCK' => string '14000' (length=5)
          public 'creationDate' => string '2019-09-05 17:27:15' (length=19)
          public 'FOTO' => string 'http://cache.megastore.com.co/MIXFAGOWQL4_280_20190905172717_5d718bc5840a6.png' (length=78)
  'productsTotal' => 
    array (size=1)
      0 => 
        object(stdClass)[66]
          public 'totalProd' => string '3' (length=1)

我需要基于product_sku值删除一个产品(对象),但是我仍然无法做到这一点,这就是我所拥有的:

 foreach($array as $k=>$v) { 
        if($k == "productInventory"){
          foreach ($array[$k] as $key=>$value) { 
            if ($key === "product_sku" && $value === "MISIKTFN71P") { 

                unset($array[$k]); //Delete from Array 
            }
          }
        }
    }

任何提示或帮助将不胜感激。

php arrays object foreach
1个回答
0
投票
foreach ($array["productInventory"] as $k => $product) {
    if ($product->product_sku === "MISIKTFN71P") {
        unset($array["productInventory"][$k]);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.