Im kinda new with php and JSON, and after digging around for a response I was not able to find one, I was hoping if you could help me…
I am working on a Report from with a JSON Response
The JSON response looks somewhat like this:
stdClass Object
(
[FlightInformation] => stdClass Object
(
[#size] => 10
[Itinerary] => Array
(
[0] => stdClass Object
(
[bookingID] => 123456789
[creationDate] => 10/04/2012
[Customer] => stdClass Object
(
[email] => someone#aol.com
)
[FlightConfirmation] => stdClass Object
(
[supplierId] => AA
[arrivalDate] => 10/04/2012
[departureDate] => 10/05/2012
[confirmationNumber] => 0987654321
[RateInformation] => stdClass Object
(
[#size] => 1
[RateDescription] => stdClass Object
(
[#promo] => false
[ChargeableInfo] => stdClass Object
(
[#total] => 57.94
I building a Report that is like this..
foreach( $response->FlightInformation->Itinerary as $output) {
echo $output-> bookingID;
echo $output-> creationDate;
echo $output-> arrivalDate; <<<< won't Print
echo $output->departureDate; <<<< won't Print
echo $output->total; <<<< won't Print
and some more elements… but anything that is under an object inside de Itinerary Array won't show..
I cannot get to print thes values indicated, please help…
Cheers
That is because arrival, departure and total are objects themselves. You would have to do something like:
$output->Customer->FlightConfirmation->arrivalDate;
$output->Customer->FlightConfirmation->departureDate
$output->Customer->FlightConfirmation->RateInformation->RateDescription->ChargeableInfo->total;
Your missing a level of nesting those elements are inside FlightConfirmation:
Related
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 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 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
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;