PHP foreach array – keep keys

You want to keep the key of each array object when you create the first foreach loop. The syntax looks like this:

foreach ($arr as $key => $value) {
    echo "{$key} => {$value} ";
    print_r($arr);
}

So for your example, it’d be something like this:

// Create array of all objects
  $AllEstatesList = array();
    foreach ($GetEstateList[0] as $EstateType => $GetEstateType) {
        foreach ($GetEstateType as $GetEstate) {
            if ($GetEstate["id"] != null) {
                $GetEstate["type"] = $EstateType;
                $AllEstatesList[] = $GetEstate;
            }
        }
    }

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top