How to delete object from array inside foreach loop?

前端 未结 6 1579
情书的邮戳
情书的邮戳 2020-11-28 21:00

I iterate through an array of objects and want to delete one of the objects based on it\'s \'id\' property, but my code doesn\'t work.

foreach($array as $ele         


        
6条回答
  •  离开以前
    2020-11-28 21:16

    You can also use references on foreach values:

    foreach($array as $elementKey => &$element) {
        // $element is the same than &$array[$elementKey]
        if (isset($element['id']) and $element['id'] == 'searched_value') {
            unset($element);
        }
    }
    

提交回复
热议问题