4

I have a PHP array in the following format:

array (size=2)
  '2019-09-28' => 
    array (size=2)
      'A' => float 4
      'B' => float 85
      'C' => float 200
  '2019-09-29' => 
    array (size=2)
      'A' => float 5
      'B' => float 83
      'C' => float 202

I'm trying to use the array_walk() function in PHP to rearrange the array keys as follows:

array (size=3)
  'A' => 
    array (size=2)
      '2019-09-28' => float 4
      '2019-09-29' => float 5
  'B' => 
    array (size=2)
      '2019-09-28' => float 85
      '2019-09-29' => float 83
  'C' => 
    array (size=2)
      '2019-09-28' => float 200
      '2019-09-29' => float 202

I'm using the following code to do so:

$result_arr = [];   
array_walk($myArray,function($v,$k) use (&$result_arr){
  $result_arr[key($v)][$k] = $v[key($v)];
});

But this code that I'm currently using produces ONLY the first item A, and doesn't produce the following items B and C. I.e. I expect the full output but only receive the first item as follows:

array (size=1)
 'A' => 
   array (size=2)
    '2019-09-28' => float 4
    '2019-09-29' => float 5

Please help. What am I missing?

4 Answers4

3

You need to apply forach() there too:

$result_arr = [];   
array_walk($myArray,function($value,$key) use (&$result_arr){
    foreach($value as $k=> $val){ //$value is an array, so iterate over it
         $result_arr[$k][$key] = $val;
    }

});

print_r($result_arr);

Output: https://3v4l.org/fJJhm

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
3

foreach solution, fiddle here:

$array = [
  '2019-09-28' => [
    'A' => 4,
    'B' => 85,
    'C' => 200,
  ],
 '2019-09-29' => [
    'A' => 5,
    'B' => 83,
    'C' => 202,
  ]
];

$res = [];
foreach ($array as $key => $item) {
    foreach ($item as $itemKey => $itemValue) {
        $res[$itemKey][$key] = $itemValue;
    }
}
print_r($res);
u_mulder
  • 54,101
  • 5
  • 48
  • 64
1

You can approach this is

 array_walk($a, function($v,$k) use (&$r){
    array_walk($v,function($v1,$k1) use (&$r,$k){
    $r[$k1][$k] = $v1;
  });
});
print_r($r);

Working example : https://3v4l.org/An23C

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

I think array_walk would be best used for modifying values in an array instead of messing with the array's structure. I'd personally use array_reduce to aggregate information of a given array to form another one with a completely different structure (i.e. your use case)

<?php

$array = [
        '2019-09-28' => [
                'A' => (float) 4,
                'B' => (float) 85,
                'C' => (float) 200,
        ],
        '2019-09-29' => [
                'A' => (float) 5,
                'B' => (float) 83,
                'C' => (float) 202,
        ],
];

$result = array_reduce(array_keys($array), function ($result, $date) use ($array) {
        return array_reduce(array_keys($array[$date]), function ($result, $key) use ($array, $date) {
                $result[$key][$date] = $array[$date][$key];
                return $result;
        }, $result);
}, []);

var_dump($result);

Demonstrated in PHP Sandbox.

Koala Yeung
  • 7,475
  • 3
  • 30
  • 50