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.

1
  • 1
    You should also stop using the mysql_ functions. They are pretty old. Better to use mysqli or PDO. Commented Dec 15, 2011 at 11:34

6 Answers 6

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

    //$results has all that you need
    print_r($results);
Sign up to request clarification or add additional context in comments.

3 Comments

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?
Well if your motive is to json_encode it. you can $jsonstr = json_encode($results); Check the link below link
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'];

Comments

0

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

$array = array();
while($row = mysql_fetch_array($result)) {
 $array[] = $row['uid'];
}

Comments

0

Try this,

while($row = mysql_fetch_assoc($result)) {
   $ar[]=$row;
}

Comments

0

Try mysql_fetch_assoc instead:

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.