PHP foreach array - keep keys

Viewed 72

I have a JSON file which structure looks like this (very simplified):

    [
      {
        "customerId": "M12345",
        "houses": [
          {
             "id": "OBJ12345_1731321200",
             "status": {
               "id": "4",
              "name": "Sold"
            },
           {
            "id": "OBJ12345_1732393235",
            "status": {
              "id": "4",
              "name": "Såld"
            }
          ],
         "plots": [
          {
            "id": "OBJ12345_1771637082",
            "status": {
              "id": "4",
              "name": "Sold"
            }
           ],
        "projects": [],
        "farms": [],
        "commercialPropertys": [],
        "condominiums": [],
        "foreignProperties": [],
        "premises": []
       }
    ]

I made this to get an array of all object but have no idea how to keep the keys "houses" or "plots"?

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

I need to figure out how to create an array that keeps the keys from parent and includes them in the new array...

The result I'm looking for is this:

    Array
        (
            [0] => Array
                (
                    [type] => houses
                    [id] => OBJ12345_1731321200
                    [status] => Array
                        (
                            [id] => 4
                            [name] => Såld
                        )

        
                )

            [1] => Array
                (
                    [type] => houses
                    [id] => OBJ12345_1732393235
                    [status] => Array
                        (
                            [id] => 4
                            [name] => Såld
                        )
                )

            [2] => Array
                (
                    [type] => plots
                    [id] => OBJ15053_1771637082
                    [status] => Array
                        (
                            [id] => 4
                            [name] => Såld
                        )

                )

        )

please help! :)

1 Answers

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;
            }
        }
    }
Related