3

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! :)

2
  • Please provide an example of a data structure you are trying to create. Commented Oct 28, 2020 at 19:15
  • Updated my question with the example of a data structure I'm are trying to create Commented Oct 28, 2020 at 20:01

1 Answer 1

2

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;
            }
        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, might work but the result is not what I'm looking for, see my new "answer" how it should look
Gotcha, I've updated my anwer to get the result you want. You should probably delete your answer and update your question with what you're looking for just to follow stack overflow etiqutte.
Perfect! Thank you very much! (I updated my question as you pointed out)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.