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;
Related
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 try to take from bing api related results and write script, who take me with print_r($value) this
Array
(
[0] => stdClass Object
(
[__metadata] => stdClass Object
(
[uri] => https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/RelatedSearch?Market='en-US'&Query='car'&$skip=0&$top=1
[type] => RelatedSearchResult
)
[ID] => 8bbe5043-f85b-41b3-b044-3649628fc5cf
[Title] => Cars Games
[BingUrl] => http://www.bing.com/search?q=Cars+Games
)
[1] => stdClass Object
(
[__metadata] => stdClass Object
(
[uri] => https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/RelatedSearch?Market='en-US'&Query='car'&$skip=1&$top=1
[type] => RelatedSearchResult
)
[ID] => b9df73ab-c410-4480-b172-2719643d1120
[Title] => Car Pictures
[BingUrl] => http://www.bing.com/search?q=Car+Pictures
)
)
How i can take [Title] and [BingUrl] from this array ? Thank you.
Use [] to access array elements and use -> to access object members.
In your case, it would be $arr[$index]->Title and $arr[$index]->BingUrl
Most APIs provide results in JSON format, so you may save yourself some headache by using json_decode($api_result, 1) to decode them as an array instead of an object.
If $value is array then you can use this code
foreach ($value as $item)
{
echo $item->Title;
echo $item->BingUrl
}
I have code for read XML SOAP 1.2 from web service.
Im using this: https://stackoverflow.com/a/18580428/2629513
I get this code below:
SimpleXMLElement Object
(
[OdkazyResponse] => SimpleXMLElement Object
(
[OdkazyResult] => SimpleXMLElement Object
(
[odkazy] => SimpleXMLElement Object
(
[odkaz] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[kod_zbozi] => 31400001
[typ] => OBR1
[popis] => Oki ML 280 - foto
[url] => http://www.atcomp.cz/katalog/31400001/ML280.gif
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[kod_zbozi] => EC376123GB
[typ] => OBR1
[popis] => Malý obrázek
[url] => http://www.atcomp.cz/katalog/EC376123GB/lq-680_-_maly.jpg
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[kod_zbozi] => EC376123GB
[typ] => OBR2
[popis] => Velký obrázek
[url] => http://www.atcomp.cz/katalog/EC376123GB/lq-680_-_velky.jpg
)
)
And how I can read the [kod_zbozi], [typ], [popis], [url] attributes? I need to save it into my mysql database (this is not problem, the problem is read the data from this format XML). Thanks.
Well, the very first thing you might want to do is to convert that object into an array (to avoid naming problems)
You can use this function to do that:
function object2array($object) {
return json_decode(json_encode($object), true);
}
then something like this:
$data = object2array(simplexml_load_string('....'));
print_r($data); // Its regular array now, use it keys to access values, then simply insert them into db