12

I can create simple json objects like this:

$d = array('item' => "$name" ,'rate' => "$rating");

But what if I want to build an array of items and do it dynamically since I am building it from a db query?

Update:

Let me be more specific I know I have to do:

$jsonCode = json_encode($d);

which will create a json object with an item and rate field. But I want multiple json objects in a json array when i encode it.

What I want json wise is something like this:

[{"item":"toy","rating":"baz" },{"item":"bike","rating":"2.3" }, {"item":"juice","rating":"1.3" }]
5
  • 11
    That is not JSON. That is a PHP array. Commented Jun 26, 2013 at 23:36
  • 11
    That's not JSON, it's just a PHP array. Commented Jun 26, 2013 at 23:36
  • 11
    That's not JSON, it is just a PHP array. :) Commented Jun 26, 2013 at 23:36
  • 28
    All together, with feeling! Commented Jun 26, 2013 at 23:37
  • 1
    Wanting multiple json objects in a json array/object sounds to me like wanting multiple arrays in an array. Just saying though. Unless you want to store json strings in an array. Not really sure though. Commented Jun 26, 2013 at 23:45

4 Answers 4

26

But I want multiple json objects in a json array when i encode it.

Then create an array of arrays and pass it to json_encode. The documentation about arrays explains how to add elements to an array, in the section Creating/modifying with square bracket syntax.

Associative arrays, like the one you already have, will be encoded as objects, "normal" arrays (arrays with consecutive numerical keys) will be encoded as arrays.

Example:

$d = array();

// This appends a new element to $d, in this case the value is another array
$d[] = array('item' => "$name" ,'rate' => "$rating");

$json = json_encode($d);
Sign up to request clarification or add additional context in comments.

1 Comment

a rare oasis where php is actually idiomatic and easy, rather than imperative and idiosyncratic.
9

This will create a multi-dimensional array from your database query, and then encode it as JSON.

$d = array();
while ($row = $stmt->fetch_assoc()) {
  $d[] = $row;
}
$json = json_encode($d);

Each $row will be an associative array of the data returned from the database. Assigning it to $d[] adds it as an indexed element of that container array.

Comments

1

What you can do is create a php array dynamically as you want then covert it into a json array as below.

$json_array = json_encode($array);

Keep in mind that what you have provided is not a json array

1 Comment

I just updated my original questions as I was not specific enough
1

Why not create your array as you just have done but then pass the array through json_encode?

If you want a multi-dimensional array, try

$array[] = array("key1" => value1, "key2" => value2);

4 Comments

Reading between the lines, his real problem is he doesn't know how to create multi-dimensional arrays.
Please read when he updated, he didn't put that he didn't know how to do that or what he exactly wanted when I posted the comment
Even at the beginning, it seemed obvious he didn't know how to create an array dynamically. He wrote what if I want to build an array of items and do it dynamically.
Edited it, should be abit better

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.