【问题标题】:Remove object from array of objects in php从php中的对象数组中删除对象
【发布时间】:2020-08-14 10:55:05
【问题描述】:

我有一个从 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_skuvalue 删除一个产品(对象),但我仍然无法做到,这就是我所拥有的:

 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】:
    foreach ($array["productInventory"] as $k => $product) {
        if ($product->product_sku === "MISIKTFN71P") {
            unset($array["productInventory"][$k]);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-04
      • 1970-01-01
      • 2014-08-24
      • 2017-04-13
      • 1970-01-01
      • 1970-01-01
      • 2022-09-29
      • 2023-01-16
      相关资源
      最近更新 更多