简体   繁体   中英

Create a multidimensional array from mysql recordset using nested foreach loop

Can someone please tell me which syntax to use to build the "directory" array from a mysql recordset? Thank you!

$directory = array();

foreach ($resultSetFromDB as $row){

    foreach ($row as $field => $value){

        $directory[$field][] = $value;

    }
}

I can print $field and $value, no problem

Try this:

$directory = array();

foreach ($resultSetFromDB as $i => $row) {

    foreach ($row as $field => $value) {

        $directory[$i][$field] = $value;

    }
}

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