0

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?

0

5 Answers 5

1

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'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

And how to display other attributes of array like office ,age etc. by using a loop?
1

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.

Comments

0

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'];

Comments

0

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.

Comments

0

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'];

1 Comment

Thank you :) Now how can i display all the elements of array in well mannered fashion?

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.