get value within object in an array - php

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;

Related

Issue with getting php objects from JSON array

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

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 wildcard for object key

I sometimes encounter and object like this:
stdClass Object
(
[batchcomplete] =>
[query] => stdClass Object
(
[pages] => stdClass Object
(
[48548] => stdClass Object
(
[pageid] => 48548
[ns] => 0
[title] => Dopamine
That object key 48548 is gonna be different every time so I have no way of knowing what its value is. Lets say I need to get the title (Dopamine) in this object, I would need to do something like this:
$title = $object->query->page->{*WILDCARD*}->title;
But I haven't figured out a way to do this yet. Is there a way to skip an object key like this without having to find out the value of the key?
A numeric object property is not going to work. Assuming there is only one, convert to an array and get the key:
$array = (array)$object->query->pages;
$title = $array[key($array)]->title;
Or just get the one element:
$title = current((array)$object->query->pages)->title;
If this is coming from JSON you might want to decode it as an array in the first place. If not, then maybe this:
$array = json_decode(json_encode($oject), true);
For non-numeric properties this should work:
$var = key(get_object_vars($object->query->pages));
$title = $object->query->pages->$var->title;

Get array value and put it into a variable (multidimensional array?)

When a print_r() an array $stats, I get the following:
Array ( [0] => Array ( [like] => 71 [dislike] => 372 [total] => 443 [like_s] => 78 [dislike_s] => 291 [total_s] => 369 [final] => 11 ))
I want to get the [dislike_s] value and put it into a variable.
I have attempted this:
$statss = $stats['dislike_s'];
But it did not work. I have also tried $statss = $stats['dislike_s'][0]; without result.
What am I doing wrong?
You are missing a level of your array
Array ( [0] => Array ( [like] => 71
//^ this level
Also you are using the wrong variable, as you said your array is stored in $stats so
$total_revision = $stats[0]['dislike_s'];
$stats is a two dimensional Array, i.e. it is an array of arrays. You can see this from the output of print_r. You have something that looks like `Array ( [0]=>Array(...)). Therefore, when accessing an element of the inside array, you can think of it like this:
$inner_array=$stats[0];
$total_revisions=$inner_array['dislike_s'];
Php gives you a shorthand for combining these steps(pretty much all languages do) and it looks like $total_revisions=$stats[0]['dislike_s'] but intuitively it's the same thing. You're saying "in the array $stats[0] give back the value of array element 'dislike_s'"

How to get data out of stdclass object?

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

Categories