'array_map triple dimensional array [duplicate]
How do I run a array_map on a triple dimensional array? Where I want to "clear" the innermost array?
It looks like this:
Array
(
[1] => Array
(
[1] => Array
(
[cat] => Hello!
[url] => hello
)
[5] => Array
(
[cat] => Good job!
[url] => good-job
)
[2] => Array
(
[2] => Array
(
[cat] => How are you?
[url] => how-are-you
)
[6] => Array
(
[cat] => Running shoes
[url] => running-shoes
)
)
)
I want to make it look like this:
Array
(
[1] => Array
(
[1] => Array
(
[cat] => Hello!
[url] => hello
)
[2] => Array
(
[cat] => Good job!
[url] => good-job
)
[2] => Array
(
[1] => Array
(
[cat] => How are you?
[url] => how-are-you
)
[2] => Array
(
[cat] => Running shoes
[url] => running-shoes
)
)
)
This solution Reset keys of array elements in php? "just" works on tow diemensional arrays, if Im not wrong.
Solution 1:[1]
you could write a short function to do it with array_map:
function mappingfunction($array){
$remappedarray = array();
foreach($array as $layer){
$remappedarray[] = array_map('array_values', $array);
}
return $remappedarray;
}
if you want to preserve the keys:
function mappingfunction($array){
$remappedarray = array();
foreach($array as $key => $layer){
$remappedarray[$key] = array_map('array_values', $array);
}
return $remappedarray;
}
Untested, but should point you in the right direction.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | tobynew |