I have a multidim array that I can view using print_r($users):
Array
(
[0] => stdClass Object
(
[username] => crustacea
[created_on] => 2010-08-07 19:54:32
[active] => 1
)
)
I can also use print_r($users[0]). Since there's only one member in $users, the output looks somewhat the same.
I can't see how to retrieve the value crustacea. echo $users[0]['username']; doesn't do it. Examples I can find are echo $users['name_of_array']['username']; -- but I don't have a name of array to work with. How do I do it?
$user[0] is not an array but an object as indicated by [0] => stdClass Object.
echo $users[0]->username;
$user[0] is object and not an array. You can access your value by doing
$users[0]->username;
Or you could cast/convert your object into an array like
((array)$users[0])['username'];
Related
Please tell me how to output the imtId value, the array is not complete, I will not output further, but the meaning should be clear.Thank you in advance
Array
(
[id] => mavrin-wildberries-1635334576193516728
[jsonrpc] => 2.0
[result] => stdClass Object
(
[cards] => Array
(
[0] => stdClass Object
(
[id] => d3c33a3f-f5b3-5647-8e7a-ad50d27d4417
[imtId] => 30306963
[userId] => 0
[supplierId] => e9b901b9-b663-5648-97b8-6313d0e245ba
[imtSupplierId] => 0
I tried:
echo ['result']['cards'][0]['nmId']
A few things:
You need to echo an actual variable, not just a series of indexes.
The item inside "result" is an object, not an array
So is the item within the "0" index.
There is no such index as "nmld" - you said you wanted "imtId" instead, so I don't know why you didn't use that?
Therefore, if this data is contained in a variable called $arr then something like
echo $arr["result"]->cards[0]->imtId;
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
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 trying to access values from a PHP object returned from an API. I am trying to get the 'total' atribute.
stdClass Object
(
[#attributes] => stdClass Object
(
[status] => ok
)
[invoices] => stdClass Object
(
[#attributes] => stdClass Object
(
[page] => 1
[per_page] => 25
[pages] => 1
[total] => 5
)
My returned object is stored in a variable called $list.
$list->invoices->attributes->total
I'm trying to echo / print_r that, but getting nothing?
Any help is appreciated!
The # is a part of the property name, you can't just ignore it.
echo $list->invoices->{'#attributes'}->total;
$total = $list->invoices->attributes()->total;
As it turns out, you don't need to even specify #attributes to read this data. The key trick to get to it is to cast the result as a string.
So, I know it seems strange, but this will work in this case:
echo (string)$list->invoices['total'];
In my case i used:
$att_side = $xml->item->attributes()->side;
I am trying to get specific data set from the object but unable to find out why I cannot call the number in my assignment. This is it as follows:
$teir = $league->data->summonerLeagues->0->teir;
First of all this is calling the data from the league which is set and so that yo can see what the data looks like here it is:
stdClass Object
(
[data] => stdClass Object
(
[summonerLeagues] => Array
(
[0] => stdClass Object
(
[queue] => RANKED_SOLO_5x5
[name] => Dr. Mundo's Crushers
[tier] => BRONZE
[requestorsRank] => III
[entries] => Array
at this point I am trying to assign the variable $teir to teir in the object but they use 0 in the object and the way I am calling it must be the issue.....
Any suggestions?
Array access is with brackets, while object properties are accessed with ->:
$tier = $league->data->summonerLeagues[0]->tier; // Fixed typo per #MikePurcell's comment