Reputation: 105
This is the decoded array which i'm getting from url using php curl method,
Array ( [result] => Array ( [0] => Array ( [id] => 1 [name] => FIRDOUS FAROOQ BHAT [office] => MG Road [age] => 25 [start_date] => 2017-04-27 22:08:11 [salary] => $20000 ) ) )
Now the problem is i'm not able to fetch a particular value from it.I used echo $result->name;
as well as var_dump['name'];
,i'm getting null value. Can anyone sort it out?
Upvotes: 0
Views: 212
Reputation: 4684
if your variable name is $data
where you are storing your this array,
echo $data['result'][0]['name'];
echo $data['result'][0]['office'];
or (if multiple data)
foreach($data['result'] as $res){
echo $res['name'];
echo $res['office']; //if office there
echo $res['age'];
}
Upvotes: 1
Reputation: 2477
Here you are getting an array but you are tried to access the object, echo $result->name;
You shouldn't use this instead use this
echo $data['result'][0]['name'];
Upvotes: 0
Reputation: 28529
You decode you json string into array, you need to use index to access array element like $result['result'][0]['name'];
. You cannot use ->
to access array element, this operator is used to access element of an object.
Upvotes: 1
Reputation: 794
Hello Here if result contains more than one element in array. In this case safe way to access your result is. And Here I am considering your response from CURL you will store inside $result
variable if you will do it like this then below code will helps you.
foreach($result['result'] as $singleArray)
{
echo $singleArray['name'];
}
Like this you can access all elements of result array.
Upvotes: 0
Reputation: 22911
If the output you've posted here is stored in $result
, you would want to access it as such:
//Get the first result, and the name from that first result
$result['result'][0]['name'];
Upvotes: 0