Php 将数组分解为多维数组(父级->;子级->;子级)
我有一个数组($categories\u final),看起来像这样:Php 将数组分解为多维数组(父级->;子级->;子级),php,arrays,multidimensional-array,explode,Php,Arrays,Multidimensional Array,Explode,我有一个数组($categories\u final),看起来像这样: Array ( [0] => Accessories/Apron [1] => Accessories/Banners [2] => Accessories/Belts [3] => Brand/Brand1 [4] => Brand/Brand2 [5] => Apparel/Men/Belts [6] => Apparel/
Array
(
[0] => Accessories/Apron
[1] => Accessories/Banners
[2] => Accessories/Belts
[3] => Brand/Brand1
[4] => Brand/Brand2
[5] => Apparel/Men/Belts
[6] => Apparel/Men/Socks
[7] => Apparel/Women/Leggings
)
我试图让它看起来像这样,这样我就可以将它添加到数据库中,作为我商店的类别结构:
Array
(
[Accessories] => Array
(
[0] => Apron
[1] => Banners
[2] => Belts
)
[Apparel] => Array
(
[0] => Men => Array
(
[0] => Belts
[1] => Socks
)
[1] => Women => Array
(
[0] => Leggings
)
)
...etc
以下是我到目前为止得到的信息,但我无法确定如何将孩子添加到Men/&Women/:
$categories = array();
foreach ($categories_final as $cat) {
$levels = explode('/', $cat);
if (isset($categories[$levels[0]])) {
if (!in_array($levels[1], $categories[$levels[0]])) {
$categories[$levels[0]][]= $levels[1];
}
}
else {
$categories[$levels[0]][]= $levels[1];
}
}
print_r($categories);
再一次,我被困在如何在他们各自的父母身上添加/腰带/袜子和/绑腿上
感谢您的帮助,谢谢 因为可以有无限数量的分支,所以您可能应该有一个递归解决方案。我尽了最大努力,得到了以下代码:
$arr = ['Accessories/Apron', 'Accessories/Banners', 'Accessories/Belts','Brand/Brand1','Brand/Brand2',
'Apparel/Men/Belts', 'Apparel/Men/Socks', 'Apparel/Women/Leggings'];
$final = [];
foreach ($arr as $branch) {
$temp = branchRecursive($branch);
$final = array_merge_recursive($final, $temp);
}
function branchRecursive($branch) {
// explode only first
$newBranch = explode('/', $branch, 2);
// A leaf, no more branches
if(count($newBranch) != 2) {
return $newBranch[0];
}
$array [ $newBranch[0] ]= branchRecursive($newBranch[1]);
return $array;
}
它返回以下内容:
Array
(
[Accessories] => Array
(
[0] => Apron
[1] => Banners
[2] => Belts
)
[Brand] => Array
(
[0] => Brand1
[1] => Brand2
)
[Apparel] => Array
(
[Men] => Array
(
[0] => Belts
[1] => Socks
)
[Women] => Leggings
)
)
与代码唯一不同的是
[Women] => Leggings
而不是
[0] => Leggings
但我想睡觉,我的脑袋不工作,所以如果有人能指出该做什么改变,我会很感激。我希望这不是什么大问题:)一种方法是将数组项“转换”为json,然后解码为数组
我首先做了一个中间步骤,其中字符串变成无效的json,但是preg_replace将内部的{}
转换为[]
使其有效。然后与结果合并
$result =[];
foreach($arr as $val){
$count = count(explode("/", $val));
$str = '{"' . str_replace('/', '":{"', $val) . '"}' . str_repeat("}", $count-1);
$json = preg_replace("/(.*)(\{)(.*?)(\})/", "$1[$3]", $str);
$result = array_merge_recursive($result, json_decode($json, true));
}
Print_r($result);
此代码使您能够创建任意数量的分支
$source = array('Accessories/Apron ' ,
'Accessories/Banners',
'Accessories/Belts',
'Brand/Brand1',
'Brand/Brand2',
'Apparel/Men/Belts',
'Apparel/Men/Socks',
'Apparel/Women/Leggings'
);
function convert($categories_final) {
$categories = array();
foreach ($categories_final as $cat) {
$levels = explode('/', $cat);
// get category
$category_name = $levels[0];
array_shift($levels);
if(!array_key_exists($category_name,$categories)) {
$categories[$category_name] = array();
}
$tmp = &$categories[$category_name] ;
foreach($levels as $index => $val){
if($index + 1 === count($levels) ){
$tmp[] = $val;
} else {
$i = find_index($tmp , $val);
if( $i == count($tmp) ) { // object not found , we create a new sub array
$tmp[] = array($val => array());
}
$tmp = &$tmp[$i][$val];
}
}
}
return $categories;
}
function find_index($array , $key) {
foreach($array as $i => $val ) {
if(is_array($val) && array_key_exists($key , $val) ){
return $i ;
}
}
return count($array);
}
print_r(convert($source));
这就是结果
Array
(
[Accessories] => Array
(
[0] => Apron
[1] => Banners
[2] => Belts
)
[Brand] => Array
(
[0] => Brand1
[1] => Brand2
)
[Apparel] => Array
(
[0] => Array
(
[Men] => Array
(
[0] => Belts
[1] => Socks
)
)
[1] => Array
(
[Women] => Array
(
[0] => Leggings
)
)
)
)
这很有趣,节省了很多空间…我选择了另一个答案,因为它更符合我的风格,但你的解决方案是我要玩的东西!非常感谢。哈这就是为什么我要问你们:)…非常感谢你们的解决方案,这正是我想要的(结合上面的一个)-再次感谢@很乐意帮忙;)谢谢你提出的有趣的高质量问题。在这里变得非常罕见;D