我有一个数组,它通过调用基于父id的递归函数出来。这个数组是一个n级多维数组。我想要的是把这个数组分解成单维的,这样每个子元素就在它们的父元素之后。我使用下面的函数首先转换成递归树。
function formatTree($tree, $parent){
$tree2 = array();
foreach($tree as $i => $item){
if($item['cat_parent_id'] == $parent){
$tree2[$item['cat_id']] = $item;
$tree2[$item['cat_id']]['submenu'] = formatTree($tree, $item['cat_id']);
}
}
return $tree2;
}
这是我的数组。
Array
(
[58] => Array
(
[cat_id] => 58
[cat_name] => Desserts
[cat_parent_id] => 0
[submenu] => Array
(
[535] => Array
(
[cat_id] => 535
[cat_name] => dessert child
[cat_parent_id] => 58
[submenu] => Array
(
)
)
)
)
[56] => Array
(
[cat_id] => 56
[cat_name] => Biryani & Rice
[cat_parent_id] => 0
[submenu] => Array
(
)
)
)
这就是我想要的。
Array
(
[0] => Array
(
[cat_id] => 58
[cat_name] => Desserts
[cat_parent_id] => 0
[submenu] => Array
(
)
)
[1] => Array
(
[cat_id] => 535
[cat_name] => dessert child
[cat_parent_id] => 58
[submenu] => Array
(
)
)
[2] => Array
(
[cat_id] => 56
[cat_name] => Biryani & Rice
[cat_parent_id] => 0
[submenu] => Array
(
)
)
)
快2020年了。我知道这有点晚了,但对于那些正在搜索的人来说:他们的答案很接近,但他们只返回父元素。我已经稍微调整了一下,使它工作(使用array_merge
)。
function array_single_dimensional($items)
{
$singleDimensional = [];
foreach ($items as $item) {
$children = isset($item['children']) ? $item['children'] : null; //temporarily store children if set
unset($item['children']); //delete children before adding to new array
$singleDimensional[] = $item; // add parent to new array
if ( !empty($children) ){ // if has children
//convert children to single dimensional
$childrenSingleDimensional = array_single_dimensional($children);
//merge the two, this line did the trick!
$singleDimensional = array_merge($singleDimensional, $childrenSingleDimensional);
}
}
return $singleDimensional;
}
$singleDimensionalArray = array_single_dimensional($multidimensionalArray);
所以,我假设你可以改变你的初始函数,使一个数组像第二个…然后你应该像这样更新你的函数:
function formatTree($tree, $parent){
$tree2 = array();
foreach($tree as $i => $item){
if($item['cat_parent_id'] == $parent){
$item['submenu'] = array();
$tree2[] = $item;
formatTree($tree, $item['cat_id']);
}
}
return $tree2;
}
应该可以。对于您的用例,您不需要在函数中使用parent_id
。
function formatTree($tree){
$tree2 = array();
foreach($tree as $i => $item){
$submenu = $item['submenu'];
unset($item['submenu']); //clear submenu of parent item
$tree2[] = $item;
if(!empty($submenu)){
$sub = formatTree($submenu); //submenu's return as array in array
$tree2[] = $sub[0]; // remove outer array
}
}
return $tree2;
}
试试这个,
$array = Array
(
"58" => Array
(
"cat_id" => 58,
"cat_name" => "Desserts",
"cat_parent_id" => 0,
"submenu" => Array
(
"535" => Array
(
"cat_id" => 535,
"cat_name" => "dessert child",
"cat_parent_id" => 58,
"submenu" => Array
()
)
)
),
"56" => Array
(
"cat_id" => 56,
"cat_name" => "Biryani & Rice",
"cat_parent_id" => 0,
"submenu" => Array
()
)
);
function singledimensional($array)
{
$res = array();
foreach ($array as $i => $item) {
$temparr = $item;
$item['submenu'] = array();
$res[] = $item;
if (!empty($temparr['submenu']) ){
$child = singledimensional($temparr['submenu']);
$res[] = $child[0];
}
}
return $res;
}
echo '<pre>';
print_r(singledimensional($array));
echo '</pre>';
输出:Array
(
[0] => Array
(
[cat_id] => 58
[cat_name] => Desserts
[cat_parent_id] => 0
[submenu] => Array
(
)
)
[1] => Array
(
[cat_id] => 535
[cat_name] => dessert child
[cat_parent_id] => 58
[submenu] => Array
(
)
)
[2] => Array
(
[cat_id] => 56
[cat_name] => Biryani & Rice
[cat_parent_id] => 0
[submenu] => Array
(
)
)
)
我希望这对你有帮助:)
(php4>= 4.0.1, php5)
array_merge_recursive -递归合并两个或多个数组
' function array_merge_recursive_distinct (array &$array1, array &$array2){
foreach ( $array2 as $key => &$value )
{
if ( is_array ( $value ) && isset ( $merged [$key] ) && is_array ( $merged [$key] ) )
{
$merged [$key] = array_merge_recursive_distinct ( $merged [$key], $value );
}
else
{
$merged [$key] = $value;
}
}
返回$合并;}div ?>