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

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.

Related

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.

How to access to my PHP var [duplicate]

This question already has answers here:
What is the syntax for accessing PHP object properties? [closed]
(3 answers)
Closed 3 years ago.
I'm lost by retrieving my PHP var. I've tried multiple things without success.
My var is called $answer and here is his var_dump :
object(stdClass)#3631 (1) { ["token"]=> string(159) "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOjEsImlhdCI6MTQ2MTMyMDQzNCwic3ViIjoiQ2hhdEF1dGhlbnRpZmljYXRpb24ifQ.18jpKLj_6Banyncyq6bz9jIFSB3IRDpBCvSgpIGJPrs" }
The most logic is to access by $answer["token"] But it's not working.
How can i get my data ?
This doesn't look like an array, but an object of stdClass. Use the Object Operator to access it:
$answer->token;
You can access it like this: $answer->token
You can access by $answer->token; instead of $answer["token"], as it is an object - not an array

Extract Values from stdclass object in php [duplicate]

This question already has an answer here:
How can I access an object property? [closed]
(1 answer)
Closed 7 years ago.
I have a stdclass object as below in PHP :-
$sample = (object) array(
"sname" => "test"
,"bselection" => "12345"
,"bind" => "1"
);
I need the output as below -
test123451
Please advise how I can I get the output as above.
Since you casted it as an object upon declaration, just access it like any other normal object, thru -> arrow operator:
echo "{$sample->sname}{$sample->bselection}{$sample->bind}";
Several versions will work as well using . or ,:
echo $sample->sname,$sample->bselection,$sample->bind;
echo $sample->sname.$sample->bselection.$sample->bind;
Try this.
You can simple access an object using -> sign and dot . sign to concatenate it.

How is PHP object called that has colons and brackets? [duplicate]

This question already has answers here:
What is the type this string? a:1:{s:2:"en";}
(3 answers)
Closed 8 years ago.
What kind of string is this? How can I unserialize it and get the array out of it?
a:2:{i:0;s:7:"Abogado";i:1;s:7:"Notario";}
This is a serialized string. You can unserialize it with this function: unserialize(), like this:
$str = 'a:2:{i:0;s:7:"Abogado";i:1;s:7:"Notario";}';
print_r(unserialize($str));
Output:
Array ( [0] => Abogado [1] => Notario )
Side Note:
A quote from the manual:
Warning:
FALSE is returned both in the case of an error and if unserializing the serialized FALSE value. It is possible to catch this special case by comparing str with serialize(false) or by catching the issued E_NOTICE.
Warning:
Do not pass untrusted user input to unserialize(). Unserialization can result in code being loaded and executed due to object instantiation and autoloading, and a malicious user may be able to exploit this. Use a safe, standard data interchange format such as JSON (via json_decode() and json_encode()) if you need to pass serialized data to the user.
I used this:
$argument = 'a:2:{i:0;s:7:"Abogado";i:1;s:7:"Notario";}';
$arr = unserialize(urldecode($argument));
print_r($arr);

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

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

Categories