1

I am trying to convert a php resultset into an array (using foreach), I'm not quite doing it right..

I have a resultset:

$result

I am looping through it as below: (horribly wrong)

while($row = mysql_fetch_array($result)) {

    foreach (mysql_fetch_array($result) as $k => $v) {
      echo "Fred's $k is ". $v['uid']. "\n";
    }

}

I want this array to be in the below format:

Array
(
    [0] => Array    //row1
        (
            [column1] => value
            [column2] => value
            [column3] => value
        .
        .

        )

    [1] => Array    //row2
        (
            [column1] => value
            [column2] => value
            [column3] => value
        .
        .
        )

    [2] => Array    //row3
        (
            [column1] => value
            [column2] => value
            [column3] => value
        .
        .
        )

)

I am a newbie, please help.

Jason
  • 15,017
  • 23
  • 85
  • 116
Kannan Lg
  • 911
  • 4
  • 11
  • 21
  • 1
    You should also stop using the mysql_ functions. They are pretty old. Better to use mysqli or PDO. – Evert Dec 15 '11 at 11:34

6 Answers6

6
    $results = array();
    while($row = mysql_fetch_assoc($result))
    {
        $results[] = $row;
    }

    //$results has all that you need
    print_r($results);
Shoogle
  • 206
  • 1
  • 13
  • thats exactly what i needed.. but is there anyway, that i can store the output of print_r to a variable so i can json_encode it? – Kannan Lg Dec 15 '11 at 05:09
  • Well if your motive is to json_encode it. you can $jsonstr = json_encode($results); Check the link below [link](http://php.net/manual/en/function.json-encode.php) – Shoogle Dec 15 '11 at 08:59
  • Or check var_export($results) [http://www.php.net/manual/en/function.var-export.php](http://www.php.net/manual/en/function.var-export.php) – Shoogle Dec 15 '11 at 09:02
2

You don't need a foreach to do this, you can just stick with your while loop:

$result_set = array();

while($row = mysql_fetch_array($result)) {
    $result_set[] = $row;
}

Then you can use it like a normal array:

$result_set[0]['column_name'];
Lucas
  • 10,476
  • 7
  • 39
  • 40
0

As demonstrated here, you do not need to call a fetching function to access the result set values.

$resultArray = [];
foreach ($resultSetObject as $row) {
    $resultArray[] = $row;
}

Assuming you have rows in your result set, this will generate an indexed array of associative arrays.

If you want to be selective about which columns are included in your output array, then you can access them by using associative array syntax to refer to the db table's column names ($row['column_a']).

If you want EVERYTHING to go into your array, then you can using mysqli's fetch_all() to skip the manual loop.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0

i can't understand well but hope this will help you :

$array = array();
while($row = mysql_fetch_array($result)) {
 $array[] = $row['uid'];
}
jogesh_pi
  • 9,762
  • 4
  • 37
  • 65
0

Try this,

while($row = mysql_fetch_assoc($result)) {
   $ar[]=$row;
}
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
0

Try mysql_fetch_assoc instead:

$results_array = array();
while($row = mysql_fetch_assoc($result)) {
    $results_array[] = $row;
}
Ayman Safadi
  • 11,502
  • 1
  • 27
  • 41