Using the below JSON fragment as an example, if I wanted to access the Value of Year, I would use something like
$result->ItemSpecifics->NameValueList[1]->Value[0] .
However, this only works if you know the index of the Year array element (1 in this case).
My question is, if I don't know what the array index of Year is, how can I still access it' value? Is there a way to find the Year element by name rather than index?
This is not correct but I would expect a solution like:
$result->ItemSpecifics->NameValueList['Year']->Value[0]
JSON Example:
[ItemSpecifics] => stdClass Object
(
[NameValueList] => Array
(
[0] => stdClass Object
(
[Name] => Returns Accepted
[Value] => Array
(
[0] => ReturnsNotAccepted
)
)
[1] => stdClass Object
(
[Name] => Year
[Value] => Array
(
[0] => 2001
)
)
[2] => stdClass Object
(
[Name] => Manufacturer
[Value] => Array
(
[0] => Porsche
)
)
I have this array from JSON file and I want to get data from [url]. Array is saved as variable $data.
stdClass Object
(
[images] => Array
(
[0] => stdClass Object
(
[startdate] => 20190625
[fullstartdate] => 201906250700
[enddate] => 20190626
[url] => /th?id=OHR.SutherlandFalls_ROW5711472757_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp
)
)
)
Result should be like:
echo $data[0]->url; will show the link/value /th?id=...
Your $data variable is not an array, it's an instance of stdClass. Therefore, you can retrieve it like this:
$data->images[0]->url;
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'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).
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;