I have a multi dimentional php array like this :
where the column "b" has 2 possible values ( x, y) and column "v" has 2 possible values ( t,f )
Array
(
[0] => Array
(
[a] => 6
[b] => x
[c] => t
)
[1] => Array
(
[a] => 4
[b] => x
[c] => t
)
[2] => Array
(
[a] => 6
[b] => y
[c] => f
)
I want to rearrange the columns so that they are structured by values , in the following way.
My Question is, is there a smart way to do this using some native php functions , without looping through everything
Array(
[value of b=x] => Array(
[value of c=t] => Array( all ids in the array)
[value of c=f] => Array( all ids in the array)
)
[value of b=y] => Array(
[value of c=t] => Array( all ids in the array)
[value of c=f] => Array( all ids in the array)
)
)
I assume that by all ids in the array
you mean "all the a
column values in the array". In which case, this is what you want:
$result = array();
array_walk($array, function($val, $key) use (&$result) {
$result[$val['b']][$val['c']][] = $val['a'];
});
print_r($result);
If by ids
you mean the outer array indices, simply change $val['a']
to $key
in the above code.