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.
Related
This question already has answers here:
SimpleXML Reading node with a hyphenated name
(2 answers)
Closed 4 years ago.
I have an XML with product. I have a problem with read values witch has a "-" in name because this method dosent work:
$productHurtoID = $product->kod-kreskowy->__toString();
Below You have $product variable:
SimpleXMLElement object {
nazwa => SimpleXMLElement object
kod-katalogowy => (string) K0428
kod-kreskowy => (string) 0027084373370
}
Thanks for help. Kind regards
You could access your dynamically created properties via
$productHurtoID = $product->{'kod-kreskowy'}->__toString();
A different approach would be to pre-process your XML input and replace hyphens with camelCase. But depending on your use-case, this might not be possible.
Regards
You can use following code for getting values with - (hyphen) in key.
$productHurtoID = $product->nazwa->{'kod-kreskowy'}->__toString();
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
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.
This question already has answers here:
PHP Object Property has brackets in it
(2 answers)
Closed 9 years ago.
I'm doing a print_r on an object that is returned after running a query. Here is my print_r log.
stdClass Object
(
[MAX(sort_order)] => 3
)
I want to get the value inside [MAX(sort_order)] but I cant figure out how to target it from within php.
Like $sort_order = $object->[MAX(sort_order)]; (I know that won't work)
Does anyone know how I can do this?
Try add this in your query MAX(sort_order) AS max_sort_order in your query
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