php foreach with multidimensional array

后端 未结 12 1616
一向
一向 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:25

    Ideally a multidimensional array is usually an array of arrays so i figured declare an empty array, then create key and value pairs from the db result in a separate array, finally push each array created on iteration into the outer array. you can return the outer array in case this is a separate function call. Hope that helps

    $response = array();    
    foreach ($res as $result) {
            $elements = array("firstname" => $result[0], "subject_name" => $result[1]);
            array_push($response, $elements);
        }
    

提交回复
热议问题