Get a PHP object property that is a number [duplicate] - php

This question already has answers here:
How to access object properties with names like integers or invalid property names?
(7 answers)
Closed 3 years ago.
I'm trying to get a property from JSON data decoded into a PHP object. It's just a YouTube data API request that returns a video object that has a content object liks so;
[content] => stdClass Object
(
[5] => https://www.youtube.com/v/r4ihwfQipfo?version=3&f=videos&app=youtube_gdata
[1] => rtsp://v4.cache7.c.youtube.com/CiILENy73wIaGQn6pSL0waGIrxMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
[6] => rtsp://v6.cache3.c.youtube.com/CiILENy73wIaGQn6pSL0waGIrxMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
)
Doing
$object->content->5
Throws "unexpected T_DNUMBER" - which makes perfect sense. But how do I get the value of a property that is a number?
I'm sure I should know this. Thanks in advance.

This should work:
$object->content->{'5'}

Another possibility is to use the 2nd parameter to json_decode:
$obj = json_decode(str, true);
You get an array instead of a PHP object, which you can then index as usual:
$obj['content'][5]

JSON encode, and then decode your object passing true as the second param in the decode function. This will return an associative array.
$array = json_decode(json_encode($object), true);
Now you can use your new array
echo $array['content']['5'];
Using $object->content->{'5'} will not work if the object was created by casting an array to an object.
A more detailed description can be found here: https://stackoverflow.com/a/10333200/58795

Related

trying to get value from array by json_encode Illegal string offset [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
Here is what I have tried:
$arrayToJson = '{"doctype":"invrec","docnum":3006}';
$arrayToJson = json_encode($arrayToJson);
print_r($arrayToJson['docnum']);
The result is: "
You've made a mistake with how you are trying to convert the object to an associative array in your PHP.
You should be using json_decode to decode the string to an object. Additionally, you want it to be an associative array so must specify TRUE for the second parameter of json_decode:
$arrayToJson = '{"doctype":"invrec","docnum":3006}';
$arrayToJson = json_decode($arrayToJson, TRUE);
print_r($arrayToJson['docnum']);
OUTPUT:
3006
For your reference, the documentation for json_decode is here.
First of all, you want to decode your JSON, not encode it a second time.
And secondly, your JSON contains an object, so you either need to use $arrayToJson->docnum here to access the property, or you need to decode it with the second parameter of json_decode set to true, so that the result will be converted into an array.

PHP stdClass Object with dot [duplicate]

This question already has an answer here:
How can I access a property with an invalid name?
(1 answer)
Closed 6 years ago.
I have a stdClass, that has an attribute with a dot in it's name. It's returned to me via an API, so there's nothing I can do with the name.
It looks like this:
stdClass Object ( [GetPlayerResult] => stdClass Object ( [RegistrationResponses.Player] => Array (...)
Now, how do I access the array? When I do this:
print_r($result->GetPlayerResult->RegistrationResponses.Player);
it (obviously) prints just "Player". I have tried putting apostrophes around the last part, using [''] syntax (like an associative array), none of that works and throws a 500 error.
Any ideas?
Thanks!
You can try using braces:
$object->GetPlayerResult->{"RegistrationResponses.Player"}
Or you can cast it to an associative array
$result = (array) $object->GetPlayerResult;
$player = $result["RegistrationResponses.Player"];
If you are parsing your website response using json_decode, note the existence of the second parameter to return as associative array:
assoc
When TRUE, returned objects will be converted into associative arrays.
Please convert your object to array by using this function
$arr = (array) $obj;
echo "<pre>";print_r($arr);exit;
by print $arr you can show elements of array.

PHP stdClass Object, how to get element with the 0 index [duplicate]

This question already has answers here:
How to access object properties with names like integers or invalid property names?
(7 answers)
Closed 7 years ago.
I have a stdClass Object like this:
print_r($myobj);
stdClass Object(
[0] => testData
);
So it is clear that I cannot get the value by $myobj->0.
For getting this value I convert the object to an array, but are there any ways to get without converting?
Try this:
$obj = new stdClass();
$obj->{0} = 1;
echo $obj->{0};
source: http://php.net/manual/en/language.types.object.php
So firstly, even though it appears from print_r()'s output that the property is literally integer 0, it is not. If you call var_dump() on the object instead, you'll see that, in fact, it looks like this:
php > var_dump($myobject);
object(stdClass)#2 (1) {
["0"]=>
string(4) "testData"
}
So how do you access a property named 0 when PHP won't let you access $myobject->0? Use the special Complex (curly) syntax:
php > echo $myobject->{0};
test
php > echo $myobject->{'0'};
test
php >
You can use this method to access property names that are not considered valid syntax.

PHP array value into second array [duplicate]

This question already has answers here:
Forcing a SimpleXML Object to a string, regardless of context
(11 answers)
Closed 9 years ago.
I've been searching around but i'm failing to find an answer to something that seems it should be simple to fix!
I'm reading products from XML and putting their data into an array on a loop, the array is called $res.
Now, i need to put a value from $res into another array for loading to the DB (magento SOAP API). But when i do this i do not get a string value i wxpect, instead i get an the first array inside the second.
Here is the problem line:
$fieldDateData = array('rts_date'=>$res[0]->BackInStockDate1);
I've tried a few different things, none have worked. I thought it would be simply enough to do this:
$data = $res[0]->BackInStockDate1;
$fieldDateData = array('rts_date'=>$data);
But sadly not, i'm unsure as to why?
Thanks,
EDIT:
This is an example of the output
Array
(
[rts_date] => SimpleXMLElement Object
(
[0] => 28/06/13
)
)
Try
$data = (string)$res[0]->BackInStockDate1;
$fieldDateData = array('rts_date'=>$data);
You need to cast the value you are setting as a string:
$data = (string) $res[0]->BackInStockDate1;
$fieldDateData = array('rts_date'=>$data);

How to check if stdClass object is empty or not in php? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to Check if an Object is empty in PHP
I have this empty object
Array (
[cart_items] => stdClass Object
(
)
)
When I use empty()/is_null() , it doesn't work. When I use sizeof($object), it returns one.
How can I check it?
Cast to an array first
$tmp = (array) $object;
var_dump(empty($tmp));
The reason is, that an object is an object and there is no useful definition of "an empty object", because there are enough classes out there, that only contains methods, but no properties. Should they considered as "empty"?
Check if count( (array)$yourObject) ) == 0.
But I'd better define my own class, and provide it with a meaningful isEmpty() method.

Categories