3
我想问问如何转换阵列象下面这样:转换阵列结构()笨帮手
$arr = array(
array(
'id' => 1,
'name' => 'Home',
'link_to' => 'home.php',
'parent' => 0,
'level' => 1
),
array(
'id' => 2,
'name' => 'About',
'link_to' => 'about.php',
'parent' => 0,
'level' => 1
),
array(
'id' => 3,
'name' => 'About Me',
'link_to' => 'about-me.php',
'parent' => 2,
'level' => 2
),
array(
'id' => 4,
'name' => 'About Us',
'link_to' => 'about-us.php',
'parent' => 2,
'level' => 2
),
array(
'id' => 5,
'name' => 'Contact Us',
'link_to' => 'contact-us.php',
'parent' => 4,
'level' => 3
),
array(
'id' => 6,
'name' => 'Blog',
'link_to' => 'blog.php',
'parent' => 0,
'level' => 1
),
);
到这一个:
$result = array(
'Home',
'About' => array(
'About Me',
'About Us' => array(
'Contact Us'
)
),
'Blog'
);
有元素“父” ID这可以告诉父数组(0 = root),并且还有元素'level'。
我需要那种数组,所以我可以使用codeigniter helper中的ul()
函数创建列表。
这是非常简单的,你自己尝试过的东西? – J0HN
你应该拼出整个数组,因为一些新元素之间的关系不太明显。看起来你想用键翻转一些值,你可以使用'array_flip'作为http://www.php.net/manual/en/function.array-flip.php – Chamilyan