简体   繁体   中英

Display values of array with the same key together

I have this array

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

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

        )

which i want to display all its elements and every array key together using an article tag.I have this

  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>
        ";
     }
  }

but the problem is the code outputs eight article tags in total.The first foreach gets the array keys of the top array but how do i do to get the values 0,1,2,3 in one article such that now i will only have two article tags for the array?.

you mean like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM