dsfsdf5646
2019-05-01 15:13
浏览 637
已采纳

从作为对象元素的数组中删除元素

I'm working on a filter system to filter out food items being displayed on a menu. Every document in the menu collection contains the name of the category (or course if you will) and an array of food items.

I've tried various solutions such as the following code which gets me closer to the desired output.

foreach($menu as $category){

   foreach($category["fooditems"] as $fooditem){

       if ($fooditem["vegetarian"] == false){
                 if (($key = array_search($fooditem, (array) $category["fooditems"])) !== false) 
                    unset($category["fooditems"][$key]);        
    }
  }
}

Before:


{
    "category" : "Starters",
    "fooditems" : [ 
        {
            "name" : "No meat",
            "vegetarian" : true,

        }, 
        {
            "name" : "Horse Meat",
            "vegetarian" : false,
        }, 
        {
            "name" : "Some more meat",
            "vegetarian" : false,
        }
    ]
}

Expected after:


{
    "category" : "Starters",
    "fooditems" : [ 
        {
            "name" : "No meat",
            "vegetarian" : true,

        }
    ]
}

Actual after:


{
    "category" : "Starters",
    "fooditems" : [ 
        {
            "name" : "No meat",
            "vegetarian" : true,

        }, 
        {
            "name" : "Some more meat",
            "vegetarian" : false,
        }
    ]
}

The problem is after it spots a non-vegetarian food item and unsets it, it stops looping. I have no idea why this occurs.

Thank you to anyone who answers :)

图片转代码服务由CSDN问答提供 功能建议

我正在使用过滤系统过滤出菜单上显示的食物。 菜单集中的每个文档都包含类别的名称(如果您愿意,还包含课程)和食物项目数组。

我尝试了各种解决方案,例如以下代码 我接近所需的输出。

  foreach($ menu as $ category){
 
 foreach($ category [“fooditems”] as $ fooditem){
  
 if($ fooditem [“vegetarian”] == false){
 if(($ key = array_search($ fooditem,(array)$ category [“fooditems”]))!== false)
 unset(  $类别[ “fooditems”] [$关键]);  
} 
} 
} 
 
   
 
 

之前:

  
 {
“  category“:”Starters“,
”fooditems“:[
 {
”name“:”No meat“,
”prime“:true,
 
},
 {
”name“  :“马肉”,“素食”:假,
},
 {
“名称”:“更多肉”,“素食”:假,
} 
] 
}  
 
   
 
 

预计之后:

  
 {
“类别”:“Starters”,\  n“fooditems”:[
 {
“名称”:“无肉”,
“素食”:true,
 
} 
] 
} 
 
   
 
 

实际之后:

  
 {
“类别”:“Starters”,
“fooditems”:[
 {
  “名字”:“没有肉”,
“素食”:真实,
 
},
 {
“名称”:“更多肉”,“素食”:虚假,
} \  n] 
} 
 
   
 
 

问题在于它发现非 素食食品和取消它,它停止循环。 我不知道为什么会这样。

感谢所有回答的人:)

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • drbouzlxb92333332 2019-05-01 15:20
    已采纳

    After decoding the JSON to an array with true you can filter and return only what is equal to true:

    $menu['fooditems'] = array_filter($menu['fooditems'],
                                      function($v) {
                                          return $v['vegetarian'] === true;
                                      });
    

    To use your loop, just track the key and unset using the full path to the array:

    foreach($menu['fooditems'] as $key => $values) {
        if($values['vegetarian'] === false) {
            unset($menu['fooditems'][$key]);
        }
    }
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题