Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through database rows and create a single array

Tags:

php

I have nooby PHP question that I can't figure out!

I loop through rows from my database:

    $data = array();

    while($row = sqlsrv_fetch_array($queryResult, SQLSRV_FETCH_ASSOC)){

        $data[] = $row;
    }

$data now contains an array within an array how can I have it so that its still just a single array?

Thanks all

like image 453
Abs Avatar asked Nov 30 '25 06:11

Abs


1 Answers

That's because each $row is an associative array. If you just want data to be an array of values from one column, specify that column:

$data = array();
while($row = sqlsrv_fetch_array($queryResult, SQLSRV_FETCH_ASSOC)){
    $data[] = $row['column_name_you_want'];
}
like image 158
takteek Avatar answered Dec 02 '25 20:12

takteek