1

I am facing a problem in php using CURL. I made a https request and I got response in multidimesional array.

[{  
 "id":"22622",
 "name":"",
 "email":"[email protected]",
 "mobileno":"",
 "birth_dt":"",
 "marital_status":"",
 "gender":"",
 "educationid":"0",
 "occupationid":"0",
 "industryid":"0",
 "incomeid":"",
 "city":"0",
 "state":"0",
 "country":"0",
 "postcode":"",
 "deviceid":"805086099499488",
 "regid":"",
 "device_type":null,
 "userstatus":"0",
 "refcode":"D1219C92",
 "new_user":"0",
 "device_token":""
 }]

Now I want to decode it and save the value of "id" in a variable.

$result=curl_exec($ch);
$books = json_decode($result, true);
echo ($books[0]['id']);

I tried the above code, but failed.

3
  • What is the error you have got ? Commented Mar 5, 2016 at 12:24
  • Thanks you for your response !BUt still having an issue. Commented Mar 5, 2016 at 12:40
  • @SivaKrishnaSamireddi check my answer ! Commented Mar 5, 2016 at 12:47

2 Answers 2

1
$books = json_decode($result, true);
foreach ($books as $book)
{
    //$book carries info of books;
    $id = $book['id'];
    ///... You can define other variables here
}
Sign up to request clarification or add additional context in comments.

1 Comment

unexpected end of file in index.php on line 124.What it means?
0

You need to use foreach() loop for this.

foreach ($a[0] as $key => $value) {
echo $key."<br>".$value;
if($key == 'id'){
    $id = $value;
}
}

echo $id;

Also, if you just want to assign the value of id to another variable, you can just right

...
$no = $a[0]->id;
echo $no
...

instead of the foreach loop.

4 Comments

How can i assign the value of "id" to some variable ,say it $number?
kindly ask a different question for it. This will violate the stack protocols
So,Is it not possible to decode ?I am beginner,sorry if my question is wrong!
the json_decode($result) decodes the json object to an array(). so decoding is done.

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.