日期:2014-05-17  浏览次数:20534 次

关于数组循环的一个问题
$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';


让它循环成这样

搜索(主菜单)
  <a href="wwww.baidu">百度</a>(子菜单)

------解决方案--------------------
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}>
<a href="<{$item.url}>"><{$item.caption}></a>(子菜单)
<{/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'], '<br>';
foreach($ar['sub'] as $v)
  echo "<a href='$v[url]'>$v[caption]</a><br>";

------解决方案--------------------
多少充都好。分析后,使用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'……