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
Related
I have an array, containing an object. I need the value of a property of the first object but somehow I get an empty value.
My array $params (from print_r) looks like this:
Array
(
[newOrderStatus] => OrderState Object
(
[name] => Canceled
[template] => order_canceled
[send_email] => 1
...
Cut off here, there are two more objects in this array.
Now if I do: echo $params[0]->name I get an empty result.
Also tried print_r($params[0], true);, empty result.
Also tried, empty result:
$status = $params[0];
echo $status->name;
What am I doing wrong here?
Thanks in advance
Well, as you said your array looks like this :
Array
(
[newOrderStatus] => OrderState Object
(
[name] => Canceled
[template] => order_canceled
[send_email] => 1
...
So there is no $param[0], you should do $param['newOrderStatus'] and then get what you want : $param['newOrderStatus']->name
You need to access object as following
$params['newOrderStatus'];
In above object you will have all child objects so you can access them by following
$params['newOrderStatus']->name;
$params['newOrderStatus']->template;
Your array $params has a key called newOrderStatus which has the object as a value you are looking for.
Looking at your example, there is value for index 0.
To get the value of the name property, you could use:
$params['newOrderStatus']->name
You can type cast it to an array like this:
$array = (array) $yourObject;
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'];
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']
I am using $this->db->get_where() to get data from database in codeigniter.
Its returning following which I got using print_r()
Its looks like array of stdClass object. Anyone who how to access values inside this array.
Array ( [0] =>
stdClass Object (
[id] => 1
[password321] => qwerty
[email123] => example#gmail.com
[username123] => xyz
)
)
It shows an array of objects. There is only one object in it.
If:
$var = $this->db->get_where();
Then:
echo $var[0]->id;
Access it like any other object.
echo $array[0]->id //1
echo $array[0]->username123 //xyz
And so on. If you have multiple objects inside the array, run it through a for loop to iterate the array.
For example:
for ($i=0;$i<sizeof($array);$i++) {
echo $array[$i]->[object property];
}
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;