1

I have an array that is looking like this:

Array
(
[0] => Array
    (
        [location_id] => 1
        [property_id] => 10
    )

[1] => Array
    (
        [location_id] => 2
        [property_id] => 20
    )

[2] => Array
    (
        [location_id] => 3
        [property_id] => 10
    )

[3] => Array
    (
        [location_id] => 4
        [property_id] => 10
    )

[4] => Array
    (
        [location_id] => 5
        [property_id] => 10
    )

[5] => Array
    (
        [location_id] => 6
        [property_id] => 20
    )

And my desired output will be like this, $array:

Array
      (
    [0] => Array
        (
            [location_id] => 6
            [location_id] => 2
            [property_id] => 20
        )

    [1] => Array
        (
            [location_id] => 3
            [location_id] => 4
            [location_id] => 5
            [property_id] => 10
        )

Basically for each property_id, add all the location_id when they correspond in the same array, like in the example.

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
bt19
  • 89
  • 6
  • 3
    The result array structure is invalid. Use a sub-array for locations – splash58 Sep 04 '19 at 15:44
  • Such output Is impossible to get because you have duplicit keys there. – slepic Sep 04 '19 at 15:45
  • 2
    Possible duplicate of [How to Sort Multi-dimensional Array by Value?](https://stackoverflow.com/questions/2699086/how-to-sort-multi-dimensional-array-by-value) – zod Sep 04 '19 at 16:35

3 Answers3

1

This is really a grouping problem rather than a sorting problem.

You can group the values using the column you want to group by, in this case "property_id", as an array key.

foreach ($array as $item) {
    $property_locations[$item['property_id']][] = $item['location_id'];
}

This will give you a result like:

[
    10 => [1,3,4,5],
    20 => [2,6]
]

Which is possible and should be fairly simple to work with. The example output you showed in the question is not possible due to duplicate array keys.

If you need a result that still has property_id and location_id labels, I think your best bet would be:

foreach ($array as $item) {
    $result[$item['property_id']]['property_id'] = $item['property_id'];
    $result[$item['property_id']]['location_id'][] = $item['location_id'];
}

Which would create $result containing:

[
    10 => [
        'property_id' => 10,
        'location_id' => [1,3,4,5]
    ],
    20 => [
        'property_id' => 20,
        'location_id' => [2, 6]
    ]
];

I think that's going to be the closest possible valid result to the desired output you showed in the question.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
0

Solution is based on that how array was created, but I see that property_id is something like key so maybe just use two dim array like that

array(2) {
[10]=>
    array(3) {
        [0]=>int(11)
        [1]=>int(12)
        [2]=>int(13)
     }
 [20]=>
     array(3) {
        [0]=>int(21)
        [1]=>int(22)
        [2]=>int(23)
     }
 }

or if you want more descriptive data structure:

array(2) {
     [10]=>
         array(2) {
              ["property_id"]=>int(10)
              ["location_ids"]=>array(3) {
                        [0]=>int(11)
                        [1]=>int(12)
                        [2]=>int(13)
              }
         }
     [20]=>
         array(2) {
              ["property_id"]=>int(20)
              ["location_ids"]=>array(3) {
                        [0]=>int(21)
                        [1]=>int(22)
                        [2]=>int(23)
              }
         } 
   }

to transform your array into something like that you need to itarate over it and build new array

0

You can not use the same key name for multiple keys in the same array

You can do it by simply iterate using foreach with array_push and group by property_id

 $r = [];
 foreach($a as $v){
 if(isset($r[$v['property_id']])){
    array_push($r[$v['property_id']]['location_id'], $v['location_id']);
  }else{
    $r[$v['property_id']] = [
        'property_id' => $v['property_id'],
        'location_id' => [$v['location_id']]
    ];
 }
}

Use array_values to re-arrange the keys of the array

print_r(array_values($r));

Live example : https://3v4l.org/qEFRV

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20