-3

I am unable to get values of array of this json... how can i have all these values seperately!!

{
    "id": "jai",
    "pwd": "123",
    "user": [
        {
            "fname": "jai",
            "lname": "gupta"
        },
        {
            "fname": "sameer",
            "lname": "seth"
        }
    ],
    "college": "vit"
}
2
  • What output are you expecting? Commented Dec 15, 2015 at 7:43
  • I am able to get all values except array. i want to store all users information to db. that's i want to do with this json Commented Dec 15, 2015 at 8:27

3 Answers 3

1
$myArray = json_decode($json, true);

var_dump($myArray['id']);
var_dump($myArray['user'][0]['fname']);
Sign up to request clarification or add additional context in comments.

Comments

0

You can decode these values from json to object.

$result = json_decode('{"id":"jai","pwd":"123","user":[{"fname":"jai","lname":"gupta"},{"fname":"sameer","lname":"seth"}],"college":"vit"}');

and acccess like below:

$result->id; 
$result->pwd;

Comments

0

You can access each "value" by accessing regular php array key value pairs.

$jsonn = '{"id":"jai","pwd":"123","user":[{"fname":"jai","lname":"gupta"},{"fname":"sameer","lname":"seth"}],"college":"vit"}';
$new = json_decode($jsonn, true);

$id = $new['id'];
$user = $new['user'];
..... and so on.

Hope this helps.

Cheers!

2 Comments

use assoc parameter when docode the json if you want to access with key index.
@check missed that, thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.