0

I have this multidimensional array that i just make

array (
  0 => 
  array (
    'warehouse_zone_id' => 1,
    'warehouse_area_id' => 1,
  ),
  1 => 
  array (
    'warehouse_zone_id' => 2,
    'warehouse_area_id' => 1,
  ),
  2 => 
  array (
    'warehouse_zone_id' => 3,
    'warehouse_area_id' => 3,
  ),
  3 => 
  array (
    'warehouse_zone_id' => 4,
    'warehouse_area_id' => 3,
  ),
)  

i want to merge the array so it become like this

array (
  0 => 
  array (
    'warehouse_zone_id' => [1,2],
    'warehouse_area_id' => 1,
  ),
  1 => 
  array (
    'warehouse_zone_id' => [3,4],
    'warehouse_area_id' => 3,
  ),
)  

i've tried array_merge, array_merge_recursive but im not getting the results that i want

4
  • I guess you want to reduce your array with the same 'warehouse_area_id' , it's more than a simple merge. Commented Feb 21, 2023 at 9:43
  • So you want to group this by user_id and warehouse_area_id, yes? Then use the combination of both as key for your result array, that helps to accumulate the data in the right place. You can use array_values on the result afterwards to re-set that to a "normal" zero-based numeric index. Commented Feb 21, 2023 at 10:18
  • just updated my array. Yes, i just want to reduce the array with the same key 'warehouse_area_id' Commented Feb 21, 2023 at 12:44
  • Somewhat related: Group array rows by one column and only create a subarray from another column if more than one value Commented Feb 22, 2023 at 21:39

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.