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;
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;
I am working on a project to get the names of an array.
The arrays seem to be multidimensional, with the added bonus of being a stdclass Object. I am trying to select a key from the provided array but seem to have no luck selecting them.
echo($response->array[shoecompany]->array[1]->name);
from the information here
stdClass Object
(
[shoe] => shoemaker
[shoecompany] => Array
(
[0] => stdClass Object
(
[shoenumber] => 1
[name] => Blank
[1] => stdClass Object
(
[shoenumber] => 2
[name] => demo
)
[2] => stdClass Object
(
[shoenumber] => certificate
[name] => certofsale
)
)
)
Nothing i do seems to pull the information i need out of this. Any ways to go about pulling, said information.
The arrays seem to be multidimensional, with the added bonus of being a stdclass Object.
Arrays and objects aren't the same things.
I let you learn more about the specifics of both if you are curious.
Regarding access, yous use brackets - '[]' - when you want to access something in an array and an arrow - '->' - when you want to access an object's property :
$array['key'];
$object->property;
In your case, since only $response and the entries in the entry showcompany - I assume it's a typo - are objects, what you should write is :
$response->shoecompany[1]->name;
Which gives you in practical use :
foreach ($response->shoecompany as $val) {
echo $val->shoenumber, ' : ', $val->name, '<br>'; // Or whatever you want to print, that's for the sake of providing an example
}
If it is more convenient for you to handle exclusively arrays, you can also use get_object_vars() to convert an object properties to an array :
$response = get_object_vars($response);
Code should be like:
echo $response->shoecomapny[1]->name;
In short, to select key inside an object you need to use "->" operator and to select key inside array use "[]".
I am very basic new php learner, i having difficulty to get nested array value, here is my json result:
stdClass Object
(
[title] => Aao Raja - Gabbar Is Back | Chitrangada Singh
[link] => stdClass Object
(
[22] => Array
(
[0] => http://r8---sn-aigllnsk.c.docs.google.com/videoplayback?mime=video%2Fmp4&id=o-AExJcTxRDvCYsfgA1cIvQDs1v-pvLhKjTPdDh67X19vz&dur=145.542&itag=22&pl=48&ip=2a03:b0c0:1:d0::2f6:c001&sparams=dur,expire,id,ip,ipbits,itag,lmt,mime,mm,mn,ms,mv,nh,pl,ratebypass,source,upn&key=cms1&sver=3&expire=1437035009&upn=9lTw9Popb18&ratebypass=yes&source=youtube&lmt=1432539432699196&fexp=901816%2C9407809%2C9408142%2C9408420%2C9408710%2C9409172%2C9412774%2C9412846%2C9413149%2C9415664%2C9415958%2C9416126%2C9416370%2C9416656&ipbits=0&signature=3547894526817B37774A7838F8B68493CDD62101.3F143C74D76E8705800445A4CD4476C4F8BCD988&cms_redirect=yes&mm=31&mn=sn-aigllnsk&ms=au&mt=1437013301&mv=m&nh=IgpwcjAzLmxocjE0KgkxMjcuMC4wLjE&utmg=ytap1
[1] =>
[2] => hd720
)
[43] => Array
(
[0] => http://r8---sn-aigllnsk.c.docs.google.com/videoplayback?mime=video%2Fwebm&id=o-AExJcTxRDvCYsfgA1cIvQDs1v-pvLhKjTPdDh67X19vz&dur=0.000&itag=43&pl=48&ip=2a03:b0c0:1:d0::2f6:c001&sparams=dur,expire,id,ip,ipbits,itag,lmt,mime,mm,mn,ms,mv,nh,pl,ratebypass,source,upn&key=cms1&sver=3&expire=1437035009&upn=9lTw9Popb18&ratebypass=yes&source=youtube&lmt=1428933984759484&fexp=901816%2C9407809%2C9408142%2C9408420%2C9408710%2C9409172%2C9412774%2C9412846%2C9413149%2C9415664%2C9415958%2C9416126%2C9416370%2C9416656&ipbits=0&signature=266C126464ECDB4CC0FF076CD41F07BCC4DA7E34.08D9F13B7BF7D92FD1E1963336CC7FB8F19FE899&cms_redirect=yes&mm=31&mn=sn-aigllnsk&ms=au&mt=1437013301&mv=m&nh=IgpwcjAzLmxocjE0KgkxMjcuMC4wLjE&utmg=ytap1
[1] =>
[2] => medium
)
I can access the Title, but can't access the Link urls:
echo $title = $json->{'title'};
echo $link = $json->{'link'}->{'22'}->{'0'};
How can access the specific link array 22
This echo $title = $json->{'title'}; works because you are accessing an object's property and using -> is the correct way.
In this case $json->{'link'}->{'22'}->{'0'} you are trying to access an array item instead an object's property, because $json->{'link'}->{'22'} is an array and not an object. In this case, you should access it in this way: $json->{'link'}->{'22'}[0]. In order to avoid this kind of issues and, when you decode your JSON to a PHP object, you can pass true as a second parameter to the function json_decode and that will convert the whole object into an array. That way, you don't need to worry about accessing elements as object's attributes, you can access them, always, as array items. So, in this case, it would be: $json["link"]["22"][0].
You're confusing the way you access objects and arrays.
Getting the title is correct via $json->title, but the link should be $json->link->{'22'}[0] - a mixture of objects and arrays.
FYI the {'name'} notation is the same as name - only required when you are including variables in your object name e.g. {$someVar . 'name'}
I suppose you use json_decode() function. Do you know that you can get an array instead of StdClass Object? So, you can use.
<?php
$php_array = json_decode($json_string, true);
I'm playing with an API that's giving me data back in JSON format that I then json_decode() to the following format:
[stockData] => stdClass Object
(
[data] => stdClass Object
(
[PS3] => stdClass Object
(
[2015-01-26T20:45:01Z] => stdClass Object
(
[AMU] => 999.76
[ZIT] => 3.63
)
)
)
[status] => stdClass Object
(
[code] => 200
[text] => ok
)
)
I need some way of getting the 2015-01-26T20:45:01Z (which changes all the time).
I've tried get_Class() on the object, eg:
get_Class($bawsaq->stockData->data->PS3) (actually in a foreach loop)
But all that's returned is: "stdClass" and not the name. How can I get the object's name?
It isn't actually the object's class: it's the name of the property that contains the stdClass object. So you'd need to get the first object property name from $bawsaq->stockData->data->PS3. Which is a bit tricky, actually.
It's nicer to work with arrays. If you use the $assoc parameter of json_decode, you can get an associative array instead of an object whenever a JSON object appears. This is much easier to deal with in PHP.
$bawsaq = json_decode($jsonData, true);
You can get the key name with key:
$dateTime = key($bawsaq['stockData']['data']['PS3']);
When you decode the JSON, use
$bawsaq = json_decode($json, true);
This will return associative arrays instead of stdClass objects for all the JSON objects. Then you can use
$keys = array_keys($bawsaq['stockData']['data'];
$date = $keys[0];
You can use get_object_vars method.
$obj = new stdClass();
$obj->field1 = 'value1';
print_r(get_object_vars($obj));
Result:
Array
(
[field1] => value1
)
You can use the second argument to json_decode. This will return the data as an associative array instead of an object list, so you could simply use
$input = json_decode($jsonInput, true);
$key = key($input['stockData']['data']['PS3']);
$data = $input['stockData']['data']['PS3'][$key];
or a foreach-loop. See also key on php.net.
Hi I have this following segment of XML:
......
<Result number="4" position="1" points="25">
<Driver driverId="button" url="http://en.wikipedia.org/wiki/Jenson_Button">
<GivenName>Jenson</GivenName>
<FamilyName>Button</FamilyName>
<DateOfBirth>1980-01-19</DateOfBirth>
<Nationality>British</Nationality>
</Driver>
......
I can use the following easily to get the GivenName:
$item->Driver->GivenName;
But when I use:
$item->Driver->FamilyName;
I get SimpleXMLElement Object ()
I have looked around and found that it might be something to do with passing it to a string but then I get nothing on screen. Not even SimpleXMLElement Object.
I don't understand as it's a sibling of GivenName and that works.
You get a SimpleXMLElement object in both cases, which you'll see if you use print_r():
print_r ($item->Driver->GivenName);
print_r ($item->Driver->FamilyName);
Outputs:
SimpleXMLElement Object
(
[0] => Jenson
)
SimpleXMLElement Object
(
[0] => Button
)
You can use an explicit cast to get the values as strings:
$givenNameString = (string) $item->Driver->GivenName;
$familyNameString = (string) $item->Driver->FamilyName;
To make PHP understand you have to give typecasting forcefully on object like below:
$givenName = (array) $item->Driver->GivenName;
$familyName = (array) $item->Driver->FamilyName;
print_r($givenName);
print_r($familyName);
OUTPUT :
Array ([0] => 'Jenson')
Array ([0] => 'Button')