PHP Variable object names? - php

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

Related

How to grab value from array

I am pulling an array from my DB that is saved in this format:
[categories] => [
{"category":"Exit Sign"},
{"category":"Leaving"},
{"category":"Illuminated"},
{"category":"Sign"},
{"category":"Red"},
{"category":"Warning Sign"},
{"category":"Above"}
]
How can I loop through each {} and get the category?
Edit: I have attempted passing each JSON array from my DB through json_decode() but I am getting the following error "json_decode() expects parameter 1 to be string, array given..." Any idea what would cause this?
Edit 2: Here is the var_dump output for just one row in my DB:
array(1) {
["categories"]=>
string(845) "[{"category":"Built Structure"},{"category":"The Americas"},{"category":"Sky"},{"category":"New York City"},{"category":"Manhattan - New York City"},{"category":"USA"},{"category":"History"},{"category":"Suspension Bridge"},{"category":"Brooklyn - New York"},{"category":"Brooklyn Bridge"},{"category":"Scenics"},{"category":"Skyscraper"},{"category":"River"},{"category":"Downtown District"},{"category":"East River"},{"category":"Cityscape"},{"category":"Bridge - Man Made Structure"},{"category":"City"},{"category":"Lighting Equipment"},{"category":"Arch"},{"category":"Urban Skyline"},{"category":"Architecture"},{"category":"Sunset"},{"category":"Night"},{"category":"Modern"},{"category":"Urban Scene"},{"category":"Tower"},{"category":"Famous Place"},{"category":"Gate"},{"category":"Outdoors"},{"category":"East"},{"category":"Travel"}]"
}
Got it! I had to pass $array['categories'] into json_decode and then it worked properly. Thanks everyone for your help!
Well, So you are new to this world. You are welcome.
Let your array value is names as $json. This value is a json format, so for access this value you need to decode them. You need to use a function called json_decode, which has two parameter, the first one is string where you pass your variable json, an second one is bool where you pass true or false.
You have to pass when you want your array as associative not object. Here i ignore the second option, so my return array is object.
After decoding your json string you have an array, now you have to use a loop foreach to browse all the elements of the array. As you can see in the resultant array below, the array is multi-dimensional so after applying a foreach loop you just reach the first depth. For getting the value of category you should need to use -> after $val which is the first depth array.
$json = '[{"category":"Exit Sign"},{"category":"Leaving"},{"category":"Illuminated"},{"category":"Sign"},{"category":"Red"},{"category":"Warning Sign"},{"category":"Above"}]';
$arr = json_decode($json); //Also you can pass $yourArr['categories'];
foreach($arr as $val){
echo $val->category."<br/>";
}
After decoding your array looks like:
Array
(
[0] => stdClass Object
(
[category] => Exit Sign
)
[1] => stdClass Object
(
[category] => Leaving
)
[2] => stdClass Object
(
[category] => Illuminated
)
[3] => stdClass Object
(
[category] => Sign
)
[4] => stdClass Object
(
[category] => Red
)
[5] => stdClass Object
(
[category] => Warning Sign
)
[6] => stdClass Object
(
[category] => Above
)
)
Result:
Exit Sign
Leaving
Illuminated
Sign
Red
Warning Sign
Above

Parsing a timestamp based PHP std class obkect

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;

get the data of an array in a stdClass Object

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'].

json_encode how to get the orginal array [stdClass Object]

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.

How to get data from array in object

I can't seem to get specific data from an array inside an object.
$this->fields->adres gets the address correctly, but i can't get a level deeper.
I've tried:
$this->fields->province
$this->fields->province->0
$this->fields->province[0]
And: (edit)
$this->fields["province"][0]
$this->fields['province'][0]
$this->data->fields['province'][0]
But it does not return anything while it should return "Flevoland".
First part of the object print_r($this, TRUE) below:
RSMembershipModelSubscribe Object
(
[_id] => 2
[_extras] => Array
(
)
[_data] => stdClass Object
(
[username] => testzz
[name] => testzz
[email] => xxxx#example.com
[fields] => Array
(
[province] => Array
(
[0] => Flevoland
)
[plaats] => tesdt
[adres] => test
You can also use type casting.
$fields = (array) $this->data->fields;
echo $fields['province'][0];
As you can see by your output, object members are likely to be private (if you follow conventions, anyway you must prepend an underscore while calling them), so you're calling them the wrong way;
This code works:
$this->_data->fields['province'][0];
You can see it in action here;
I created a similar object, and using
$membership = new RSMembershipModelSubscribe();
echo $membership->_data->fields['province'][0];
outputs "Flevoland" as expected.
As fields is already an array, try this:
$this->fields['province'][0]
This assuming the [_data] object is $this.
Fields and province are both arrays, you should be trying $this->fields["province"][0]
$this->_data->fields['province'][0]

Categories