foreach ループ内の配列からオブジェクトを削除するにはどうすればいいですか? 質問する

foreach ループ内の配列からオブジェクトを削除するにはどうすればいいですか? 質問する

オブジェクトの配列を反復処理し、その 'id' プロパティに基づいてオブジェクトの 1 つを削除したいのですが、コードが機能しません。

foreach($array as $element) {
    foreach($element as $key => $value) {
        if($key == 'id' && $value == 'searched_value'){
            //delete this particular object from the $array
            unset($element);//this doesn't work
            unset($array,$element);//neither does this
        } 
    }
}

何か提案があればお願いします。

ベストアンサー1

foreach($array as $elementKey => $element) {
    foreach($element as $valueKey => $value) {
        if($valueKey == 'id' && $value == 'searched_value'){
            //delete this particular object from the $array
            unset($array[$elementKey]);
        } 
    }
}

おすすめ記事