php - 从php中的对象数组中删除对象
问题描述
我有一组从 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
}
}
}
}
任何提示或帮助将不胜感激。
解决方案
foreach ($array["productInventory"] as $k => $product) {
if ($product->product_sku === "MISIKTFN71P") {
unset($array["productInventory"][$k]);
}
}
推荐阅读
- html - 带有滚动的 ui 内 100% 宽度
- python - Selenium 登录另一个会话将注销其他会话
- python - def count_dominators(项目)。在列表中查找支配者。如何让这个更有效率?
- java - 如何使用流框架发送 HTTP 请求
- android - 带有 ViewModel 的 Android Hilt 范围为 Navgraph,Provider Factory 问题
- r - 类似于cox回归风险模型,我们可以使用survivalsvm得到生存曲线和风险比吗?
- url - Laravel FormRequest 从 url 获取参数
- sql - 从 sql BigQuery 中的数组对象中获取键值对
- javascript - jQuery/TypeScript/JavaScript 类中的延迟语句
- amazon-web-services - Gitlab CI管道在同一阶段并行运行作业并调用/触发同一阶段的其他作业