yozhik
yozhik

Reputation: 5084

multidimensional arrays in php?

I have a problem with arrays in PHP. So, lets see: I have a table "Users" in database with such fields: "name, surname, age, rating". The number of users is nearly 100. I need to get all of them from database, post it to array, encode them with JSON end show. So I suppose, I need to do the follows:

  1. Get one row from DB.
  2. Add all data to some array fields like associative array.
  3. Push that array to some array container.
  4. Encode array container to JSON.

But when I try to encode, I get only last element in array container. I writted something like that:

$arrContainer = array();
$arr = array();
$i = 0;
while($row = getDataFromDB)
{
  arr[$i] = $i;
  arr["name"] = row["name"];
  arr["age"] = row["age"];

  array_push($arrContainer, $arr);
  $i++;
}

JSON.encode($arrContainer);

QUESTION: How can I make array of arrays with some data?

Upvotes: 0

Views: 156

Answers (5)

yozhik
yozhik

Reputation: 5084

$allValuesArr = array();
$arr= array();
while($myRow = mysql_fetch_array($result))
{
    $arr["uid"] = $myRow["uid"];
    $arr["name"] = $myRow["name"];
    $arr["surname"] = $myRow["surname"];
    $arr["age"] = $myRow["age"];
    $arr["telephone"] = $myRow["telephone"];
    $arr["prof_id"] = $myRow["prof_id"];
    //$allValuesArr[] = $arr;
    array_push($allValuesArr, $arr);
}

$strJSON = json_encode($allValuesArr);
echo $strJSON;

response I get:

[{"uid":"1","name":"Sergii","surname":"Surname1","age":"26","telephone":"0976543135","prof_id":"1225423"},{"uid":"2","name":"Slava","surname":"Surname2","age":"24","telephone":"06353524","prof_id":"13584384"},{"uid":"3","name":"Desame","surname":"Surname3","age":"28","telephone":"0973153584","prof_id":"35843815"}]

Upvotes: 0

klennepette
klennepette

Reputation: 3196

The code you have posted is correct, however in the following lines you forgot to put $ before the $arr variable:

arr[$i] = $i;
arr["name"] = row["name"];
arr["age"] = row["age"];

Also the line arr[$i] = $i; should probably be something like $arr['rowNum'] = $i; having the key be the same as the value seems redundant.

And it should be said that using $arrContainer[]=$row; as @RiaD suggested is a quicker way to get an array of the row, array_push() is more intended to add multiple values to an array or if you want to treat it as a stack.

Upvotes: 1

TigerTiger
TigerTiger

Reputation: 10806

You can create array as in @RiaD's answer and then output json as ..

while($row = getDataFromDB) // @RiaD's code
{
   $arr[]=$row; // add $row as element of $arr array
}

//json output
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); 
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); 
header("Cache-Control: no-cache, must-revalidate" ); 
header("Pragma: no-cache" );
header('Content-type: application/json');
echo  json_encode($your_array);

Upvotes: 1

user409460
user409460

Reputation:

try this:

$arr = array();
$i = 0;
while($row = getDataFromDB){
  $arr[$i] = $row;
  $i++;
}
JSON.encode($arr);

Upvotes: 1

RiaD
RiaD

Reputation: 47658

while($row = getDataFromDB)
{
   $arr[]=$row; // add $row as element of $arr array
}

Now you get and can Json encode it

Besides, some database extensions can returns all data to multidimensional array automatically.
See PDOStatement::fetchAll manual

Upvotes: 3

Related Questions