I have a two dimensional array like this:
$array = [
[..., "key"=>"key 1",...],
[..., "key"=>"key 2",...],
[..., "key"=>"key 2",...],
[..., "key"=>"key 3",...],
[..., "key"=>"key 3",...],
[..., "key"=>"key 3",...],
];
I want a three dimensional array from this array, splitted by the same "key" value,
like this:
$array_output = [
[
[..., "key"=>"key 1",...],
],
[
[..., "key"=>"key 2",...],
[..., "key"=>"key 2",...],
],
[
[..., "key"=>"key 3",...],
[..., "key"=>"key 3",...],
[..., "key"=>"key 3",...],
],
];
You can do that with array-reduce. Consider this example:
Now
$res
will have your output. Notice that the order of the keys in$arr
in not relevant.Finally, This function fulfilled my expectation:
However, this returns with Key Value pair, like:
but that cause no problem for me :)