-2

I have an array similar like:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Allow all
            [geo_group_id] => 1
            [geo_location_id] => 5
            [geo_name] => Afghanistan

        )

    [1] => Array
        (
            [id] => 1
            [name] => Allow all
            [geo_group_id] => 1
            [geo_location_id] => 34
            [geo_name] => Brazil

        )

    [2] => Array
        (
            [id] => 1
            [name] => Allow all
            [geo_group_id] => 1
            [geo_location_id] => 52
            [geo_name] => Costa Rica

        )

    [5] => Array
        (
            [id] => 2
            [name] => Local Geo
            [geo_group_id] => 2
            [geo_location_id] => 108
            [geo_name] => India

        )

    [6] => Array
        (
            [id] => 2
            [name] => Local Geo
            [geo_group_id] => 2
            [geo_location_id] => 105
            [geo_name] => Ireland

        )

    [7] => Array
        (
            [id] => 2
            [name] => Local Geo
            [geo_group_id] => 2
            [geo_location_id] => 162
            [geo_name] => Namibia

        )

)

And want to rearrange array like:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Allow all
            [geoLocation] => Array
                (
                    [0] => Array
                        (
                            [geo_group_id] => 1
                            [geo_location_id] => 5
                            [geo_name] => Afghanistan

                        )
                    [1] => Array
                        (
                            [geo_group_id] => 1
                            [geo_location_id] => 34
                            [geo_name] => Brazil

                        )
                    [2] => Array
                        (
                            [geo_group_id] => 1
                            [geo_location_id] => 52
                            [geo_name] => Costa Rica

                        )

                )

        )

    [1] => Array
        (
            [id] => 2
            [name] => Local Geo
            [geoLocation] => Array
                (
                    [0] => Array
                        (
                            [geo_group_id] => 2
                            [geo_location_id] => 108
                            [geo_name] => India

                        )
                    [1] => Array
                        (
                            [geo_group_id] => 2
                            [geo_location_id] => 105
                            [geo_name] => Ireland

                        )
                    [2] => Array
                        (
                            [geo_group_id] => 2
                            [geo_location_id] => 162
                            [geo_name] => Namibia

                        )

                )

        )

)

any help would be greatly appreciated. Thanks.

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125

2 Answers2

1

This seems like it's just a 'do my work for me' question.

This isn't pretty but it should work. I'm not sure how to explain it better than the official docs for foreach.

I decided on the simplest most readable solution so you could see how easy the first iteration of your solution could be. Make it work, make it pretty, make it fast, in that order.

<?php

$locations = [
      [
          'id' => 1,
          'name' => 'Allow all',
          'geo_group_id' => 1,
          'geo_location_id' => 5,
          'geo_name' => 'Afghanistan'

      ],
      [
          'id' => 1,
          'name' => 'Allow all',
          'geo_group_id' => 1,
          'geo_location_id' => 34,
          'geo_name' => 'Brazil'

      ],
      [
          'id' => 1,
          'name' => 'Allow all',
          'geo_group_id' => 1,
          'geo_location_id' => 52,
          'geo_name' => 'Costa Rica'

      ],
      [
          'id' => 2,
          'name' => 'Local Geo',
          'geo_group_id' => 2,
          'geo_location_id' => 108,
          'geo_name' => 'India'

      ],
      [
          'id' => 2,
          'name' => 'Local Geo',
          'geo_group_id' => 2,
          'geo_location_id' => 105,
          'geo_name' => 'Ireland'

      ],
      [
          'id' => 2,
          'name' => 'Local Geo',
          'geo_group_id' => 2,
          'geo_location_id' => 162,
          'geo_name' => 'Namibia'
      ]
];

$results = [];

foreach($locations as $location) {
    $id = $location['id'];

    if (!isset($results[$id])) {
        $results[$id] = [
            'id'   => $id,
            'name' => $location['name']
        ];
    }

    $results[$id]['geoLocation'][] = [
        'geo_group_id'    => $location['geo_group_id'],
        'geo_location_id' => $location['geo_location_id'],
        'geo_name'        => $location['geo_name']
    ];
}
plumpNation
  • 142
  • 1
  • 12
0

Your question is a bit ambiguous about the identifiers that you want to use. I am going to assume you want to use id and name as dual identifiers. If this is not true, you should clarify your question. And as plumpNation stated, you must always show your coding attempt when posting a question.

Either way, the most popular technique you'll find on SO uses unique temporary keys, then re-index after looping. (plump's method doesn't re-index the temporary keys after the loop.) I am using array_splice() and implode() to make my code DRYer.

Code: (Demo)

foreach($locations as $a){
    $keys=array_splice($a,0,2);  // extract dual identifiers, reduce $a to geo_ elements
    $k=implode($keys);  // convert identifying elements to string for temp key
    if(!isset($result[$k])){$result[$k]=$keys;}  // store identifying elements via temp key
    $result[$k]['geolocation'][]=$a;  // store geo_ element via temp key
}
var_export(array_values($result));  // remove temp keys / re-index the array
mickmackusa
  • 43,625
  • 12
  • 83
  • 136