显示具有相同键的数组的值

时间:2013-09-11 07:11:46

标签: php arrays

我有这个数组

[multiv] => Array
        (
            [31603] => Array
                (
                    [0] => one
                    [1] => two
                    [2] => three
                    [3] => four
                )

            [18992] => Array
                (
                    [0] => five
                    [1] => six
                    [2] => seven
                    [3] => eight
                )

        )

我希望使用article标记将所有元素和每个数组键显示在一起。我有这个

  foreach( $main_array['multiv'] as $key => $value ) {
     foreach( $value as $k => $v ) {
        echo "
           <article class='crud_list'>
              <input type='hidden' name='$key' />
              <input type='text' name='$k' value='$v' /><br/>
              <input type='checkbox' name='$k' value='$v' /><br/>
              <input type='radio' name='$k' value='$v' /><br/>
              <select><option>$k</option></select><br/>
           </article>
        ";
     }
  }

但问题是代码总共输出了8个article标记。第一个foreach获取顶部数组的数组键但是如何获取值0,1,2,3 in一个article,现在我只有两个文章标签?

1 个答案:

答案 0 :(得分:3)

你的意思是:

foreach($main_array['multiv'] as $key=>$value){
    //add your article tag
    echo "<article class='crud_list'>";
        foreach($value as $k=>$v){
            //add your inputs
            echo "<input type='hidden' name='$key' />";
            //rest of input
        }
    echo "</article>";
}  // end of first foreach