关于数组循环的一个有关问题
时间:2021-07-01 10:21:17
帮助过:27人阅读
关于数组循环的一个问题
$config['menu']['member']['coption'] = '搜索';
$config['menu']['member']['sub'][0]['caption']='百度';
$config['menu']['member']['sub'][0]['url']='http://www.baidu.com';
$config['menu']['member']['sub'][1]['caption']='谷歌';
$config['menu']['member']['sub'][1]['url']='http://www.google.com';
$config['menu']['member']['sub'][2]['caption']='SOSO';
$config['menu']['member']['sub'][2]['url']='http://www.soso.com';
让它循环成这样
搜索(主菜单)
百度(子菜单)
------解决方案--------------------Array
(
[menu] => Array
(
[member] => Array
(
[coption] => 搜索
[sub] => Array
(
[0] => Array
(
[caption] => 百度
[url] => http://www.baidu.com
)
[1] => Array
(
[caption] => 谷歌
[url] => http://www.google.com
)
[2] => Array
(
[caption] => SOSO
[url] => http://www.soso.com
)
)
)
)
)这样看不就清楚了吗
smarty
<{$arr.menu.member.sub.coption}>(主菜单)
<{foreach key=key item=item from=$arr.menu.member.sub}>
"><{$item.caption}>(子菜单)
<{/foreach}>
------解决方案--------------------PHP code
$config['menu']['member']['coption'] = '搜索';
$config['menu']['member']['sub'][0]['caption']='百度';
$config['menu']['member']['sub'][0]['url']='http://www.baidu.com';
$config['menu']['member']['sub'][1]['caption']='谷歌';
$config['menu']['member']['sub'][1]['url']='http://www.google.com';
$config['menu']['member']['sub'][2]['caption']='SOSO';
$config['menu']['member']['sub'][2]['url']='http://www.soso.com';
$ar = $config['menu']['member'];
echo $ar['coption'], '
';
foreach($ar['sub'] as $v)
echo "$v[caption]
";
------解决方案--------------------
多少充都好。分析后,使用foreach就可以。
------解决方案--------------------
顶帖收藏~~
------解决方案--------------------