Hello I have this json:
[{"org_src":"img\/base\/logo.png","src":"\/cache\/300-logo.png"},
{"org_src":"\/img\/l2.JPG","src":"\/cache\/6l-2.JPG"},
{"org_src":"\/img\/studio\/desk.JPG","src":"\/cache\/desk.JPG"},
...
How looks the array before its is decoded?
If I use Json_encode to this, I will get this:
Array
(
[0] => stdClass Object
(
[og_src] => img/base/logo.png
[src] => /cache/300-logo.png
)
[1] => stdClass Object
(
[og_src] => /img/l2.JPG
[src] => /cache/6l-2.JPG
)...
What is stdClass Object?
Thanks for any help in advance.
with json_decode($test,true); I will get this:
Array
(
[0] => Array
(
[og_src] => img/base/logo.png
[src] => /cache/logo.png
)
[1] => Array
(
[og_src] => /img/studio/l2.JPG
[src] => /cache/6l-2.JPG
...
This do not help me to see how the origin array looks like.
Here is the answer. That what I looked for. Thanks for any suggestion.
$stack[0][org_src]= "Hallo";
$stack[0][src] = "scrkjh";
$stack[1][org_src] = "Halfgfglo";
$stack[1][src] = "scrkjh";
json_encode($stack);
You probably meant json_decode
If you look in the docs, you will notice that there is assoc parameter if you want to get associative array instead of an object.
So you should do
$data = json_decode($data, true);
if you don't want objects
stdClass is the base class of all objects in PHP. If functions like json_decode() create objects that are not of a special class type or other data types will be casted to object, stdClass is used as the data type.
You can compare it to the Object data type in Java.
You asked for an example how to access stdClass's properties, or in general, object properties in PHP. Use the -> operator, like this:
$result = json_decode($json);
// access 'src' property of first result:
$src = $result[0]->src;
stdClass is php's generic empty class, kind of like Object in Java or object in Python
It is useful for anonymous objects, dynamic properties, etc. and Stdclass Object is its object
See Dynamic Properties in PHP and StdClass for example
Also.. As Marko D said, You can use json_decode($data, true); true to get an associative array instead of object.
Related
I have a PHP standard class object converted from json_decode of a REST call on an API which looks like :
Array
(
[1437688713] => stdClass Object
(
[handle] => Keep it logically awesome.
[id] => 377748
[ping] => stdClass Object
(
[url] => https://api.me.com
[id] => 377748
[name] => web
[active] => 1
[events] => Array
(
[0] => data_new
[1] => data_old
)
So far i had no issues in parsing any of the PHP objects. However this one is failing because i can not access the nested object elements using a key since 1437688713 is not assigned to a key and accessing an object is failing if i try to do this:
$object->1437688713->handle
Is there a way to access these elements ?
Update: one more thing, i would never know this value (1437688713) in advance. Just like a key. All i get is a stdclass object which i have to parse.
The outer part of your data is an array, not an object. Try:
$array['1437688713']->handle;
or if you don't know the key, you can iterate over the array (handy if it may contain multiple objects too):
foreach ($array as $key => $object) {
echo $key; // outputs: 1437688713
echo $object->handle; // outputs: Keep it logically awesome.
}
Get the first item from $object array
$first_key = key($object);
Use it with your response array,
$object[$first_key]->handle;
Or, the first element of array
$first_pair = reset($object)->handle;
I'm a novice programmer and I'm a bit lost rigth now. I would like to get the data from "gameId" and save it into a variable.
printing $recentgames:
print_r($recentgames);
returns me this:
stdClass Object (
[summonerId] => 40300606,
[games] => Array (
[0] => stdClass Object (
[gameId] => 2102575613 ...
I want to store "2102575613" into $gamesid. But my code does not work.
$gamesid = $recentgames->array[0]->gameId;
//I have also tried this code
$gamesid = $recentgames->object->games->array[0]->gameId;
//and this one
$gamesid = $recentgames->$object->$games->$array[0]->gameId;
Any help would be highly appreciated.
This should work:
$recentgames->games[0]->gameId
You access objects with $object->property and array like $array['key'].
I need to access the array coordinates in this:
object stdClass Object (
[type] => Point
[coordinates] => Array (
[0] => 53.64203491
[1] => -8.52337036
)
)
But no matter what I try I can't get it. Any ideas? I am using PHP.
If the name of the variable is $data, then you can access the the coordinates array as follows:-
$coordinatesArr = $data->coordinates;
I think this should work. If it does not, please share the peice of code you are using.
Try something like this: $stdObject->coordinates[0] and $stdObject->coordinates[1].
I have the following JSON object which I have decoded. I know that to access the data from the object, you would normally:
$value = $obj->data
Consider the following, where is a variable returned from the curl request
stdClass Object
(
[<account_name>] => stdClass Object
(
[id] => 123
[name] => "Some Name"
[email] = "someguy#somemail.net"
)
)
How do I get the values of [id], [name], [email] etc, if I do not know the object key?
If you use json_decode, set the second parameter to true to decode the json data to an array.
Then you could use foreach to loop the array even you do not know the key.
This function will most likely be able to help you. Get_Object_vars
When I print_r() the SimpleXMLElement Object referenced by variable $xmlObject, I see the following structure:
SimpleXMLElement Object
(
[#attributes] => Array
(
[uri] => /example
)
[result] => SimpleXMLElement Object
(
[message] => Record(s) added successfully
[recorddetail] => Array
(
[0] => SimpleXMLElement Object
...
)
)
)
Notice how the $xmlObject->result->message property looks like it is just a string. However, if I do print_r($xmlObject->result->message), I get the following:
SimpleXMLElement Object
(
[0] => Record(s) added successfully
)
So at this point I'm confused. Why is $xmlObject->result->message being identified as an instance of SimpleXMLElement Object in this case, when the result of printing the full $xmlObject doesn't suggest this?
And how do I actually access this value? I've tried $xmlObject->result->message[0], but it just prints out the same thing (i.e. the last code snippet I posted).
The representation you get when using print_r or var_dump on a SimpleXMLElement has very little to do with how it is structured internally. For instance there is no property #attributes you could access with $element['#attributes']['uri'] either. You just do $element['uri']
This is simply the way it is. SimpleXmlElement objects behave different. Make sure you read the examples in the PHP Manual before using SimpleXml:
http://php.net/manual/en/simplexml.examples-basic.php
To understand the implementation it in detail, you'd have to look at the source code:
http://lxr.php.net/opengrok/xref/PHP_TRUNK/ext/simplexml/simplexml.c
To print $xmlObject->result->message you just do echo $xmlObject->result->message. That will autocast the SimpleXmlElement to string.