0
    [
      {
        "status": true,
        "data": [
          {
            "id": 2,
            "name": "Algeria",
            "country_code": "DZ",
            "regions": [
              {
                "id": 2,
                "country_id": 2,
                "region_code": "RG01",
                "depots": [
                  {
                    "id": 5,
                    "region_id": 2,
                    "depot_code": "DP04",
                    "depot_name": "North Depot",
                    "area": [
                      {
                        "id": 1,
                        "depot_id": 5,
                        "area_code": "AR1",
                        "area_name": "Area-1"
                      },
                      {
                        "id": 2,
                        "depot_id": 5,
                        "area_code": "AR2",
                        "area_name": "Area-2"
                      }
                    ]
                  },
                  {
                    "id": 6,
                    "region_id": 2,
                    "depot_code": "DP06",
                    "depot_name": "east Depot",
                    "area": [
                      {
                        "id": 3,
                        "depot_id": 6,
                        "area_code": "AR3",
                        "area_name": "Area-3"
                      }
                    ]
                  }
                ]
              },
              {
                "id": 3,
                "country_id": 2,
                "region_code": "RG02",
                "depots": []
              }
            ]
          }
        ]
      }
    ]

 

i want to merge area into single array like

    "area": [
      {
        "id": 1,
        "depot_id": 5,
        "area_code": "AR1",
        "area_name": "Area-1"
      },
      {
        "id": 2,
        "depot_id": 5,
        "area_code": "AR2",
        "area_name": "Area-2"
      },
      {
        "id": 3,
        "depot_id": 6,
        "area_code": "AR3",
        "area_name": "Area-3"
      }
    ]

 

i don't want to use multiple foreach. thanks in advance.

4
  • make a collection from your array, and then use merge method on that new created collection, you will get the idea. See laravel docs laravel.com/docs/7.x/collections#method-merge Commented Jun 2, 2020 at 16:06
  • 1
    @porloscerrosΨ It's not like this. I tried a full day to solve this problem. But I'm tired now. And also resolved the issue through nested loops.But after seeing the response of APIs. I changed my mind. Because each API has different responses. So I want to make a common function for that.I need some help for resolve this problem.thats all. Commented Jun 2, 2020 at 16:53
  • Ok, no problem, it was just a suggestion so you have a better chance of getting an answer. I see that someone already wrote an answer. Good luck! Commented Jun 2, 2020 at 16:56
  • @porloscerrosΨ I'll keep it in my mind next time thanks Commented Jun 2, 2020 at 17:02

2 Answers 2

2

You can use the following code to recursively iterate of all your structure and extract only data you need (by key). I assume $data has your original data structure and within $result you will get array of area:

$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($data), RecursiveIteratorIterator::SELF_FIRST);

$result = [];
foreach ($it as $k => $v) {
    if ($k === 'area') {
        $result = array_merge($result, $v);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
1

I would not overcomplicate things, loop through you data structure. Add all areas to a collection. Prepare your data structure and i added a check to secure there is no duplicates with unique().

$data = 'your-data';
$areas = collect();

foreach($data->data as $country) {
    foreach($country->regions as $region) {
        foreach ($region->depots as $depot) {
            $areas->concat($depot->area);
        }
    } 
}

$result = ['area' => $areas->unique('id')->all()];

Comments

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.