Search code examples
phparrayslaravelunset

Why I can not unset array in associative array php


Why I can not unset array in associative array php I see the dump but Also I see unseted arrays in final array

foreach($dossier['program'] as $program){
    foreach($program['cities'] as $city){
        foreach($city['services'] as $service){
            foreach($service['featureds'] as $key=>$featured){
                dump($key);
                if($key!==(int)$quotation_conditions['groups']){
                    dump($service['featureds'][(string)$key]);
                    unset($service['featureds'][(string)$key]);
                }
            }
        }
    }
}
dd($dossier);

Solution

  • Try this

    foreach($dossier['program'] as &$program){
        foreach($program['cities'] as &$city){
            foreach($city['services'] as &$service){
                foreach($service['featureds'] as $key=>$featured){
                    dump($key);
                    if($key!==(int)$dossier['groups']){
                        dump($service['featureds'][(string)$key]);
                        unset($service['featureds'][(string)$key]);
                    }
                }
            }
        }
    }