Issue with getting php objects from JSON array - php

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?

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

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.

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

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.

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

Related

How to access stdClass Multi level object or array?

I had to make a particularly complex SOAP request in PHP and seem to have received back objects within objects. I need to get a particular value for example "session_token". I can var_dump the request and even turn it in to an array but I can't access individual elements. Please help!
Objects:
stdClass Object ( [login_response] => stdClass Object
( [response_context] => stdClass Object
( [session_token] => b1043dcb82625701188ffff03572
[response_status] => OK [response_message] => Login successful
))))
Converted array:
Array ( [0] =>
Array ( [response_context] =>
Array ( [session_token] => b1043dcb82625701188ffff03572
[response_status] => OK [response_message] => Login successful
) ) )
Once it's an array, it's JUST an array, e.g.
$obj->foo->bar->baz
will simply be
$arr['foo']['bar']['baz']
So in your case
$arr[0]['response_context']['response_status'] -> "OK"
If you leave it as an object, and there is not reason not to.
echo $obj->login_response->response_context->session_token;
echo $obj->login_response->response_context->response_status;
echo $obj->login_response->response_context->response_message;
will output
b1043dcb82625701188ffff03572
OK
Login successful
Normally to access any key of an object, need to call it like this
echo $obj->key;
But, as this is a multi-level object, to access the element, you have to code like bellow.
echo $obj->login_response->response_context->session_token;
And the result will be b1043dcb82625701188ffff03572
As same, normally to access any key of an array, need to call it like this
echo $arr['key'];
But, as this is a multi-level array, to access the element, you have to code like bellow.
echo $arr[0]['response_context']['session_token'];
And the Output is b1043dcb82625701188ffff03572
Here,
0 is the key of $arr array.
response_context is the key of 0.
session_token is the key of response_context.
You can use this service, array visualizer, this will help you target and extract only what you need. Just past the print_r output in it. That's it.
Give it a try

php encode json correctly and use it with my special array

I've experienced a problem when developing for a minecraft server: I have an array like this which is received as json and encoded but for processing the output of this I need to know how to access the array and echo one of the values for Example the Version Key.I tried to save the array as variable $json and to access it via echo $json->Version; but this results in the output of this error: Notice: Trying to get property of non-object in /home/bs-web/joomla/api/test2.php on line 12
HereĀ“s the Array:
Array
(
[0] => stdClass Object
(
[GameName] => MINECRAFT
[Version] => 1.8
[Plugins] =>
[Map] => BungeeCord_Proxy
[Players] => 7
[MaxPlayers] => 100
[HostPort] => 25565
[HostIp] => 188.40.97.86
[Software] => Vanilla
)
)
Your variable $json is an array, you cannot get property of an array. Assuming you have only one element in the array as in your example, you should use
echo $json[0]->Version;
to display the version(1.8 in your example).
once you saves the $json variable as an array, you only can access its values passing the key that you want to use, and then, point to the object. In your case, to get the Version property, you will need to use $json[0]->Version.
To access and echo all values of your object you can use a foreach loop to iterate with it.
foreach ($json as $object){
echo $object->GameName;
echo $object->Version;
echo $object->Plugins;
.... // All properties that you want
}
Hope it helps you, hugs.
You could also use json_decode($json, true) then you get an assocative array which you can access like this $json['Version']

Access a session value

Here is the Print_r of a $_SESSION variable. I am trying to access the value of user_id.
Array (
[userPieUser] => loggedInUser Object (
[email] => xxxxx#hotmail.com
[hash_pw] => xxxxxxxxx
[user_id] => 3
[clean_username] => scott
[display_username] => scott
[remember_me] => [remember_me_sessid] => c13348e6d296b8d96797eed631b20ad13f58e60af00760620327b019e4773c2d6
)
)
I have tried a dozen or so ways to get that value in PHP, however no luck. such as looping through and doing if ($key = 'user_id'){ echo $value } but that just returns the first element in the array. I'm sure it is rudimentary, however appreciate the help.
The one you're looking for is:
$_SESSION['userPieUser']->user_id
As it is part of the userPieUser object.
You should try:
echo $_SESSION['userPieUser']->user_id;
fyi: There is object withing array 'userPieUser'.
Access the variable like so:
echo $_SESSION['userPieUser']['user_id'];
PHP supports accessing object indicies like this within other object indicies.

Get value of nested key name in php array

I have been working with parsing some remote JSON with PHP. I have been able to download the JSON and assign it to a variable, and I have used the array functionality with json_decode:
$data = json_decode($remotejson, true);
I have then printed the complete array back to verify the contents of the array:
echo print_r($data);
The array prints back and I can see the keys and values:
[files] => Array
(
[/photogalleryupload.thumbs/1934307_000001.jpg] => Array
(
[source] => derivative
[format] => Thumbnail
[original] => moviefile_1934307.mp4
)
I am trying to get the value of the first nested key name which is "/photogalleryupload.thumbs/1934307_000001.jpg" and assign it to a variable.
For example, I would like the following code:
echo $data['files'][0];
To return this:
/photogalleryupload.thumbs/1934307_000001.jpg
This does not work.
The difficulty I am having is that this value I am trying to return is a 2nd level key name and I have been having trouble finding a way of assigning it to a variable.
$keys = array_keys($data['files'])
$key = $keys[0]

Trying to print or echo an array in an array

I have an array called $results, when I use function:
print_r($results);
I get the following.
Array
(
[0] => ProfileElement Object
(
[name] => John thomson
[email] => johnt#gmail.com
[Bio] => 20 years of engineering expertise
[url] => http://twitter.com
)
)
My goal is to echo [name] [email] [Bio] [url] values separately. But when I write the following code in php I don't get any values?
echo $results[0]["ProfileElement Objects"]["Bio"];
Does anyone know why? Isn't this an array inside an array?
It appears that the array element contains an object, not another array. To access the object property, use the -> operator:
echo $results[0]->Bio;
You were close.
echo $results[0]->bio;
Is probably what you want. $result[0] is an object.
Also, depending on visibility, you may need to use a getter method.
remove ["ProfileElement Objects"]
echo $results[0]->Bio;
Try doing:
$results[0]->name;
ProfileElement Object is the object type.
It's an object inside an array. It looks like you should be able to access it as $results[0]->name, $results[0]->email, etc.
It is an object. You can get the 'Bio' value using:
echo $results[0]->Bio;

Categories