I'm trying to get a value from stdClass Object array with no success.
Here is the code I'm running:
$myjson =
'{"2":{"label":"","value":"","type":null,"validation":null,"required":null},
"6":{"label":"files","value":"getThisValue","type":"file0","validation":null,"required":null},
"3":{"label":"location","value":"val3","type":"hidden","validation":"","required":"0"}
,"0":{"custom3":"zz","value":"","label":""},"1":{"custom3":"zz","value":"","label":""}
}';
$json = json_decode($myjson);
echo $json[6]->'value';
This is doesn't work, If I Print_r the JSON after decoding (print_r($json)), the array will look like this:
stdClass Object (
[2] => stdClass Object ( [label] => [value] =>
[type] => [validation] => [required] => )
[6] => stdClass Object (
[label] => files [value] => getThisValue [type] => file0 [validation]
=> [required] => )
[3] => stdClass Object ( [label] => location [value] => val3 [type] => hidden [validation] => [required] => 0 )
[0]
=> stdClass Object ( [custom3] => zz [value] => [label] => )
[1] => stdClass Object ( [custom3] => zz [value] => [label] => ) )
I need the Value: getThisValue. Any idea how I can get it? (I tried many options with no success).
Try echo $json["6"]["value"];
But for this you have to use json_decode($myjson, true); true, to get an array.
Because it's going to be two arrays inside each other and not an object you have to use 2 brackets.
You can't use a std object as an array. But in order to get your code working just add this line:
$json = get_object_vars($json);
After this you can access it like this:
echo $json[6]->value;
You could add true to your json_decode, like this: json_decode($myjson, true);, it will now convert your json object to an associative array.
And from there on you can get to the value you want by requesting the key, just like other arrays.
$newArray = json_decode($myjson, true);
echo $newArray['something'];
If you want to get the values from stdObject without converting it to array, you can do it like this:
echo $json->{'6'}->value
You can use the {'property_name'} notation to get a value of a class property with non-standard name (for ex. a number).
Related
I'm trying to get the values from [iso_3166_1] and [title] in an array, which is inside another array.
I have searched and tried several solutions and tips, but none of them works, and now I'm stuck.
The content is fetched like this
$content = json_decode($jsonFile);
Then I have the following content
stdClass Object (
[id] => 508947 [titles] => Array (
[0] => stdClass Object ( [iso_3166_1] => FR [title] => Devenir rouge [type] => )
[1] => stdClass Object ( [iso_3166_1] => MX [title] => Red [type] => )
[2] => stdClass Object ( [iso_3166_1] => LV [title] => Es sarkstu [type] => )
[3] => stdClass Object ( [iso_3166_1] => NO [title] => Rød [type] => )
[4] => stdClass Object ( [iso_3166_1] => SE [title] => Röd [type] => )
[5] => stdClass Object ( [iso_3166_1] => PE [title] => Red [type] => )
)
)
I have tried to make a foreach loop like this for instance, but that only gives me Warning: Invalid argument supplied for foreach():
foreach($content as $row) {
foreach($row['titles'] as $country) {
echo $country['iso_3166_1']['title'];
}
}
I have also tried tho following, to try get rid of the stdClass Objects in the array, which does not seem to work either:
$content = json_decode(json_encode($content), true);
$content = (array)$content;
this should work for you to compare & understand why your code fails.
$titles = $content->titles;
foreach ($titles as $country_instance) {
echo $country_instance->iso_3166_1;
echo '-';
echo $country_instance->title;
echo '<br>';
}
If I understand the question right, json_decode associative mode should return arrays of arrays from your JSON.
$array = json_decode($json, true);
You can also try using flag JSON_OBJECT_AS_ARRAY but from what I've read it only does something when null is supplied as the second argument.
how get value feild[id] in php code
stdClass Object ( [List_inserted] => Array ( [0] => stdClass Object ( [ID] => 145001 [value] => 40 ) ) [Sucssess] => 1 [ErrorMassage] => OK )
You didn't give us a name of stdClass so I'm assuming it's $stdClass.
$stdClass->List_inserted[0]->ID
Let's break it down;
stdClass Object ( [List_inserted] => Array ( [0] => stdClass Object ( [ID] => 145001 [value] => 40 ) ) [Sucssess] => 1 [ErrorMassage] => OK )
We access objects with a -> and we access arrays with []
The first part tells us it's an object, so it's;
$stdClass->List_inserted
List_inserted is an array thanks to the => Array. We can access this with [0].
$stdClass->List_inserted[0]
Well, List_inserted[0] is an object, thanks too [0] => stdClass Object; and you wanted to access the ID? So we need another ->
$stdClass->List_inserted[0]->ID
I trying to get a array in VB.NET, but I have troubles for deserialize, I don't know if my format is bad or what, but first the data is a std object
Array
(
[0] => stdClass Object
(
[id] => 6797892
[marca] => xxx
[details] => yyy
[price] => rrr
[info] => Array
(
[0] => stdClass Object
(
[Items] => Array
(
[0] => stdClass Object
(
[Por] => 1
[$$hashKey] => 03F
)
)
[Tipo] => mouse
[price] => 1.65
[$$hashKey] => 03D
)
[1] => stdClass Object
(
[Items] => Array
(
[0] => stdClass Object
(
[Por] => o
[$$hashKey] => 03J
)
)
[Tipo] => teclado
[price] => 1.65
[$$hashKey] => 03H
)
)
[$$hashKey] => 03B
)
)
next i use json_encode(in php):result is:
[{"Id":"6797904","marca":"xxx","Pais":"yyy","Liga":"rrr","Jornada":"3","info":[{"Items":[{"Por":"1","Cuota":"2.25","$$hashKey":"03I"}],"Tipo":"mouse","price":2.25,"$$hashKey":"03G"}],"$$hashKey":"03E"}]
and using
....
file_put_contents($file, print_r($current, true) );
...
I save it in items.txt, and I load it in vb.net, but I don't know what is the correct way to convert into an array using:
Dim str As JArray = JArray.Parse(TextBox1.Text)
Dim results As Object = str("Id").ToString
To put in it an answer format.
The use of print_r is not correct here.
...
file_put_contents($file, print_r($current, true) );
...
The definition for print_r as per php.net:
print_r — Prints human-readable information about a variable.
This means extra characters are added to make it readable to humans, but not for machines. It even creates invalid JSON, causing VB.NET to generate an error.
Update your code to
...
file_put_contents($file,$current);
...
And it should work
I have a JSON array that I want to be able to drill down to a lower level and print just that value. The problem occurs when I reach a level that has is indacted as [0] (or [n]). For example I have the following output, and I want to just print the game key for the first league.
This is how I am trying to print it
HtmlSpecialChars(print_r($user->fantasy_content->users[0]->user[1]->games[0]->game[0]->game_key,1))
However I keep getting this error:
Cannot use object of type stdClass as array
When I do it incrementally it seems to fail on this command (so I assume I'm not index correctly):
$user->fantasy_content->users[0]
Here is the output:
stdClass Object
(
[fantasy_content] => stdClass Object
(
[xml:lang] => en-US
[yahoo:uri] => /fantasy/v2/users;use_login=1/games
[users] => stdClass Object
(
[0] => stdClass Object
(
[user] => Array
(
[0] => stdClass Object
(
[guid] => IYEZUHTVBYRLIB3OAQC5WRZPQY
)
[1] => stdClass Object
(
[games] => stdClass Object
(
[0] => stdClass Object
(
[game] => Array
(
[0] => stdClass Object
(
[game_key] => 147
[game_id] => 147
[name] => Baseball
[code] => mlb
[type] => full
[url] => http://baseball.fantasysports.yahoo.com/b1
[season] => 2006
)
)
)
[count] => 1
)
)
)
)
[count] => 1
)
[time] => 52.390813827515ms
[copyright] => Data provided by Yahoo! and STATS, LLC
[refresh_rate] => 60
)
)
For objects you must use the -> syntax and if the key/property name is a number or has other special characters, you will need to use the $object->{'0'} syntax.
The game_key can be retrieved using:
$user->fantasy_content->users->{'0'}->user[1]->games->{'0'}->game[0]->game_key;
You can convert a stdClass object to an array by casting it like so:
<?php
$array = (array) $myObject;
echo json_encode($array);
You can also cast inline:
<?php
echo json_encode((array) $object);
I have got this result using json decode from an api call. but I dont know how to extract "VALUE" from this result..
$obj=json_decode($json_string);
print_r($obj);
stdClass Object ( [status] => OK [data] => stdClass Object ( [trends] => stdClass Object ( [rank] => Array ( [0] => stdClass Object ( [date] => 201011 [value] => 7196 ) ) ) [trends_low_sample] => [query_cost] => 1 [trends_frequency] => monthly ) )
I need only "7196" from this result. how do I do this??
Ah! Based on your updated code you're tring to get the value from PHP not Javascript? Personally I use json_decode($json_string,true); to get an associative array (json_decode), if you do that it should be accessible as:
echo $obj["data"]["trends"]["rank"][0]["value"];
As an object it's accessible as:
echo $obj->data->trends->rank[0]->value;