'How to add item in array using nested for each loop in php

I am trying to create array of row object from table in PHP. I am using the below code to do it, but I am not getting the expected result.

I want the below format:

[{"fName":"apple","fprice":11,"quantity":2},{"dName":"orange","fprice":31,"quantity":6}]

But using the following code I am getting only the last row last column value:

$query = 'select fname,fprice,imgpath,fdesc,cid from food_data';
$stid = oci_parse($conn, $query);
oci_execute($stid, OCI_DEFAULT);
while ($row = oci_fetch_assoc($stid)) {
    foreach ($row as $key=>$val) {
        //echo $item." <br />";
        $data = array(
            array(''.$key.''=>''.$val.'') );
    }
    //echo "\n";
}


Solution 1:[1]

$query = 'select fname,fprice,imgpath,fdesc,cid from food_data';
$stid = oci_parse($conn, $query);
oci_execute($stid, OCI_DEFAULT);
$res = array();
while ($row = oci_fetch_assoc($stid)) {
    $res[] = $row;
}

After that $res will contain what you want.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 angelcool.net