goblin2986
goblin2986

Reputation: 1009

JSON format in PHP

{
  "required_items": [{
      "filename": "abcd",
      "no": "3"
    },
    {
      "filename": "abc",
      "no": "2"
    }
  ]
}

I am not getting the code of the JSON format in PHP - I want to insert the filename and no through a loop.

Upvotes: 1

Views: 2969

Answers (2)

Jon Benedicto
Jon Benedicto

Reputation: 10582

If you're asking how to generate the above JSON code in PHP, do something like this:

$object->required_items = array();

for( ... your loop here ... )
{
    $item->filename = 'filename';
    $item->no = 1;
    $object->required_items[] = $item;
}

$json = json_encode( $object );

Upvotes: 1

Paul Dixon
Paul Dixon

Reputation: 300845

Pass the JSON to json_decode and you'll end up with an regular PHP data structure you can operate on. Use var_dump to take a look at it. When you're done manipulating it, turn it back into JSON with json_encode

I don't know if it's typos on your part, but the format you pasted isn't valid JSON.

Upvotes: 2

Related Questions