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'].
Related
I have an array with stdClass objects as below. How would I count how many I have under Interviewee_Name?
Tried counting as I would do an array but get an error that I can't becaused of the stdClass Object and then unsure how to proceed from there
Array
(
[0] => stdClass Object
(
[Interviewee_Name] => Array
(
[0] => stdClass Object
(
[id] => rn_Interviewee_Name_DIR_27
[value] => Janusz Jasinski
)
)
)
This'll get the number of elements under Interviewee_Name, but it'll count all elements, not just objects.
count($arr[0]->Interviewee_Name)
However, if you really only want to get the objects in Interviewee_Name, you'll need to array_filter the array to get only the objects, and then count that new array:
count(array_filter($arr[0]->Interviewee_Name, function ($el) {
return (gettype($el) == 'object');
}));
The syntax for getting an element from an array looks like $arr['index'], but in this case Interviewee_Name is a property of an object so you need to use the object syntax: $obj->prop
$array = json_decode(json_encode($formData), True);
I added that to before looking to do a count and it worked!
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
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.
I'm still new at PHP and I can't seem to count the number of Objects within another object. The stdClass object looks like this:
stdClass Object (
[data] => Array (
[0] => stdClass Object (
[Code] => ABC
[Title] => Alphabet
[sections] => Array (
[0] => stdClass Object (
[Name] => Sounds
[sections] => Vowels
)
)
)
)
I must count the number of elements in this object so i can echo it properly. For the data, I was able to do it:
$number = count($hanap->data);
I don't know how to do it for the sections.
$number = count($hanap->data->sections); // does not work.
Thanks. Any help will be greatly appreciated. :)
this will solve your problem, just cast the object to array and count it
$total = count((array)$obj);
PHP: Count an stdClass object
count($hanap->data[0]->sections)
You are missing the first member of the array where they are...
$number = count($hanap->data[0]->sections)