php foreach with multidimensional array

后端 未结 12 1549
一向
一向 2020-11-27 05:10

I\'m developing a php app that uses a database class to query mySQL.

the class is here: http://net.tutsplus.com/tutorials/php/real-world-oop-with-php-and-mysql/

相关标签:
12条回答
  • 2020-11-27 05:28

    You can use array_walk_recursive:

    array_walk_recursive($array, function ($item, $key) {
        echo "$key holds $item\n";
    });
    
    0 讨论(0)
  • 2020-11-27 05:29

    I know this is quite an old answer. Here is a faster solution without using foreach:

    Use array_column

    print_r(array_column($array, 'firstname')); #returns the value associated with that key 'firstname'
    

    Also you can check before executing the above operation

    if(array_key_exists('firstname', $array)){
       print_r(array_column($array, 'firstname'));
    }
    
    0 讨论(0)
  • 2020-11-27 05:34
    foreach ($parsed as $key=> $poke)
    {
        $insert = mysql_query("insert into soal 
                              (pertanyaan, a, b, c, d, e, jawaban)
                              values
                              ('$poke[question]',
                              '$poke[options][A]',
                              '$poke[options][B]',
                              '$poke[options][C]',
                              '$poke[options][D]',
                              '$poke[options][E]',
                              '$poke[answer]')");
    }
    
    0 讨论(0)
  • 2020-11-27 05:35

    You can use foreach here just fine.

    foreach ($rows as $row) {
        echo $row['id'];
        echo $row['firstname'];
        echo $row['lastname'];
    }
    

    I think you are used to accessing the data with numerical indicies (such as $row[0]), but this is not necessary. We can use associative arrays to get the data we're after.

    0 讨论(0)
  • 2020-11-27 05:37

    To get detail out of each value in a multidimensional array is quite straightforward once you have your array in place. So this is the array:

    $example_array = array(
    array('1','John','Smith'),
    array('2','Dave','Jones'),
    array('3','Bob','Williams')
    );
    

    Then use a foreach loop and have the array ran through like this:

    foreach ($example_array as $value) {
    echo $value[0]; //this will echo 1 on first cycle, 2 on second etc....
    echo $value[1]; //this will echo John on first cycle, Dave on second etc....
    echo $value[2]; //this will echo Smith on first cycle, Jones on second etc....
    }
    

    You can echo whatever you like around it to, so to echo into a table:

    echo "<table>"
        foreach ($example_array as $value) {
        echo "<tr><td>" . $value[0] . "</td>";
        echo "<td>" . $value[1] . "</td>";
        echo "<td>" . $value[2] . "</td></tr>";
        }
    echo "</table>";
    

    Should give you a table like this:

    |1|John|Smith   |
    |2|Dave|Jones   |
    |3|Bob |Williams|
    
    0 讨论(0)
  • 2020-11-27 05:38

    Example with mysql_fetch_assoc():

    while ($row = mysql_fetch_assoc($result))
    {
        /* ... your stuff ...*/
    }
    

    In your case with foreach, with the $result array you get from select():

    foreach ($result as $row)
    {
        /* ... your stuff ...*/
    }
    

    It's much like the same, with proper iteration.

    0 讨论(0)
提交回复
热议问题