1
{
  "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.

3
  • After your edit, your JSON string was still not valid. Also, please read meta.stackexchange.com/questions/22186/… Commented Jun 26, 2010 at 13:44
  • 1
    (tipp) jsonlint.com Commented Jun 26, 2010 at 13:49
  • 1
    Yes, after I edited your source code. Before that, it wasn't. @Gordon: Nice tip, thanks! Commented Jun 26, 2010 at 14:27

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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 );

1 Comment

Good day, sir. I tried to use your code. But my output displays only one row from my database (that contains 3 rows). Can you please help me? Thanks. Here's my question. stackoverflow.com/questions/11806959/…

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.