如何使用foreach获取值

时间:2016-03-05 04:14:20

标签: php arrays

我正在尝试根据用户输入动态创建菜单。我有一个像这样的数组,我想通过它们的索引获取每个值,或者我想使用这些值来按顺序创建菜单:

$menu['main_menu']= array(
  'menu_name' =>'main_menu',
  'menu_item1'=>array('home' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'menu_id'=>'new_menu' )),
  'menu_item2'=>array('development' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'0' )),
  'menu_item3'=>array('php' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'development' )),
);

4 个答案:

答案 0 :(得分:2)

请尝试以下代码:

foreach ($menu['main_menu'] as $item => $menu_item) {
    if (is_array($menu_item)) {
        foreach ($menu_item as $menu_name => $menu_attr) {
            echo "menu name: " . $menu_name;
            echo '<br/>';
            foreach ($menu_attr as $attr => $val) {
                echo $attr . "->" . $val;
                echo '<br/>';
            }
        }
    }
    else{
        echo $item.": ".$menu_item;
        echo "<br />";  
    }
} 

答案 1 :(得分:1)

$menu['main_menu']= array(
  'menu_name' =>'main_menu',
  'menu_item1'=>array('home' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'menu_id'=>'new_menu' )),
  'menu_item2'=>array('development' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'0' )),
  'menu_item3'=>array('php' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'development' )),
  'menu_item4'=>array('php2' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'development' )),
  'menu_item5'=>array('development2' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'0' )),
  'menu_item6'=>array('php2' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'development2' )),
);

$submenus = [];
$mainMenu = [];
$menuStart;
$menuEnd;

//prapring data
foreach($menu['main_menu'] as $item){
    if(is_array($item)){
        foreach($item as $link){
            if(isset($link['sub_menu']) & $link['sub_menu'] != '0'){
                $submenus[] = [
                    'name' => key($item),
                    'sub_menu' => $link["sub_menu"],
                    'body' => "<li class='{$link["Class name"]}'><a href='{$link["URL"]}' class='{$link["Class name"]}' id='{$link["menu_id"]}'>" . key($item) . "</a>",
                    'end' => "</li>"
                        ];
            } else {
                $mainMenu[] = [
                    'name' =>  key($item), 
                    'body' => "<li class='{$link["Class name"]}'><a href='{$link["URL"]}' class='{$link["Class name"]}' id='{$link["menu_id"]}'>" . key($item) . "</a>",
                    'end' => "</li>"
                        ];
            }
        }
    } else {
    $menuStart = "<ul class='{$item}'>";
    }
}

/// menu generatig
$menuEnd = '</ul>';
echo $menuStart;
foreach($mainMenu as $menu){
    echo $menu['body'];
        foreach($submenus as $submenu){
            if($submenu["sub_menu"] == $menu['name']){
                echo "<ul>";
                echo $submenu['body'];
                echo "</ul>";
            }    
        }
    echo $menu['end'];
}
echo $menuEnd;

答案 2 :(得分:1)

以下代码可能会对您有所帮助。

 $menu['main_menu']= array(
      'menu_name' =>'main_menu',
      'menu_item1'=>array('home' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'menu_id'=>'new_menu' )),
      'menu_item2'=>array('development' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'0' )),
      'menu_item3'=>array('php' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'development' )),
    );



    foreach($menu['main_menu'] as $menu_name=>$menu_value){

      echo "<br><br>menu_name ". $menu_name;

      if(is_array ( $menu_value )){

            foreach($menu_value as $k=>$v){
               if(is_array ( $v )){
                   foreach($v as $key=>$value)
                            echo "<br>key=".$key."  value=".$value;

              }

            }
        }
    }

答案 3 :(得分:1)

认真对待

foreach($menu['main_menu'] as $menu_name=>$menu_value)
{
  if(is_array($menu_value))
  {
     foreach($menu_value as $value)
     {
      if(is_array ( $value ))
      {
            echo "<a href='{$value["URL"]}' class='{$value["Classname"]}'>" . key($menu_value) . "</a><br />"; // 
      }
    }
  }
}