I have the following PHP object, which I want to access specific values from it but I have difficulties in understanding how to start:
kamranahmedse\Geocode Object (
[service_url:kamranahmedse\Geocode:private] => http://maps.googleapis.com/maps/api/geocode/json?sensor=false
[service_results:kamranahmedse\Geocode:private] => stdClass Object (
[results] => Array (
[0] => stdClass Object (
[address_components] => Array (
[0] => stdClass Object (
[long_name] => 112
[short_name] => 112
[types] => Array (
[0] => street_number
)
)
[1] => stdClass Object (
[long_name] => Imittou
[short_name] => Imittou
[types] => Array (
[0] => route
)
)
[2] => stdClass Object (
[long_name] => Cholargos
[short_name] => Cholargos
[types] => Array (
[0] => locality
[1] => political
)
)
[3] => stdClass Object (
[long_name] => Cholargos
[short_name] => Cholargos
[types] => Array (
[0] => administrative_area_level_5
[1] => political
)
) ...
For example how can I access:
[long_name] => Cholargos
The variable you're trying to access is private and therefore you're not going to be able to access it directly.
A better way is to use the methods provided by the class you're using.
Looking at the code you've provided I can see that you're using https://github.com/kamranahmedse/php-geocode
Here's an example of how it can be used to get the locality:
<?php
require __DIR__."/vendor/autoload.php";
use kamranahmedse\Geocode;
$address = "Imittou 112 Cholargos 155 61 Greece";
$geocode = new Geocode($address);
echo $geocode->getLocality();
Also, looking at the class source code we can see that when the address information is retrieved and local variables populated the long_name is used to populate the locality, which is what you want:
$this->locality = $component->long_name;
So the above code example should answer your question.
Related
I have the following from my code:
stdClass Object ( [orders] => Array ( [0] => stdClass Object ( [shipping_address] => stdClass Object ( [first_name] => John [last_name] => Doe [company] => [address_1] => 3927 Walnut Grove [address_2] => [city] => Rosemead [state] => CA [postcode] => 90001 [country] => US ) ) [1] => stdClass Object ( [shipping_address] => stdClass Object ( [first_name] => chris [last_name] => koh [company] => [address_1] => 745 bow [address_2] => [city] => diamond [state] => CA [postcode] => 90015[country] => US ) ) ) )
how would i extract just the first_name from both elements?
John and chris
The "Array" you get back is a mishmash of Arrays and Objects, objects are accessed differently.
Array values are accessed by
$an_array = array('apple','banana');
$an_array[0]; //will return apple
While Object values area accessed by
$an_object->key;
Take a look at your return object
stdClass Object ( //<-- Object
[orders] => Array ( //<-- Array
[0] => stdClass Object ( //<-- Object
[shipping_address] => stdClass Object ( //<-- Object
[first_name] => John
etc..
So to get for example the first name you can access it with:
$arrayAndObjects->orders[0]->shipping_address->first_name
Hope that helps you understand it a bit, here are links to the php documentation for Arrays & Objects
http://php.net/manual/en/language.types.object.php
http://php.net/manual/en/language.types.array.php
I am using PHP to convert a list of postcodes + County into latitude and longitude. I have tried using the google map api and the simplexml_load_file() function like so:
$url ="http://maps.googleapis.com/maps/api/geocode/xml?address=WS9+8NS,+West+Midlands&sensor=false";
$result = simplexml_load_file($url);
print"<pre>";
print_r($result);
Now this works fine as I am able to get the following:
SimpleXMLElement Object
(
[status] => OK
[result] => SimpleXMLElement Object
(
[type] => postal_code
[formatted_address] => Walsall, Walsall, West Midlands WS9 8NS, UK
[address_component] => Array
(
[0] => SimpleXMLElement Object
(
[long_name] => WS9 8NS
[short_name] => WS9 8NS
[type] => postal_code
)
[1] => SimpleXMLElement Object
(
[long_name] => Walsall
[short_name] => Walsall
[type] => Array
(
[0] => locality
[1] => political
)
)
[2] => SimpleXMLElement Object
(
[long_name] => Walsall
[short_name] => Walsall
[type] => postal_town
)
[3] => SimpleXMLElement Object
(
[long_name] => West Midlands
[short_name] => West Mids
[type] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[4] => SimpleXMLElement Object
(
[long_name] => United Kingdom
[short_name] => GB
[type] => Array
(
[0] => country
[1] => political
)
)
)
[geometry] => SimpleXMLElement Object
(
[location] => SimpleXMLElement Object
(
[lat] => 52.6030230
[lng] => -1.9175368
)
[location_type] => APPROXIMATE
[viewport] => SimpleXMLElement Object
(
[southwest] => SimpleXMLElement Object
(
[lat] => 52.6013713
[lng] => -1.9188871
)
[northeast] => SimpleXMLElement Object
(
[lat] => 52.6040692
[lng] => -1.9161892
)
)
[bounds] => SimpleXMLElement Object
(
[southwest] => SimpleXMLElement Object
(
[lat] => 52.6020654
[lng] => -1.9182516
)
[northeast] => SimpleXMLElement Object
(
[lat] => 52.6033751
[lng] => -1.9168247
)
)
)
)
)
Problem now is, how should I take out the first latitude / longitude and not the whole thing, I only need these two values.
Please note that the lat/long are dynamic as the address changes for each search.
From the print_r you can see the structure of the content. Then, it is a matter of following it.
This contains the values:
$result->result->geometry->location
So to print them separately, you can say:
print "lat: " . $result->result->geometry->location->lat;
print "lng: " . $result->result->geometry->location->lng;
All together:
$url ="http://maps.googleapis.com/maps/api/geocode/xml?address=WS9+8NS,+West+Midlands&sensor=false";
$result = simplexml_load_file($url);
print "lat: " . $result->result->geometry->location->lat . " <br/>";
print "lng: " . $result->result->geometry->location->lng . " <br/>";
Better to use their JSON API.
$url ="http://maps.googleapis.com/maps/api/geocode/json?address=WS9+8NS,+West+Midlands&sensor=false";
$result = file_get_contents($url);
print"<pre>";
$resultArray = json_decode($result, true);
print_r($result);
Then just access like an array!
e.g.
$resultArray['results']['geometry']['location']['lat']
$resultArray['results']['geometry']['location']['lng']
I would use cURL instead of file get contents as it gives more control. But this works fine.
I would also check the $resultArray before attempting to do anything with it.
I'm totally newbie what comes to programming so i'm not even quite sure if my terms are right but i would like to get some hints and tips what is best practice to loop through JSON object? Let's say i want all game names from following JSON print_r output.
stdClass Object
(
[_total] => 555
[_links] => stdClass Object
(
[self] => https://api.twitch.tv/kraken/games/top?limit=2&offset=0
[next] => https://api.twitch.tv/kraken/games/top?limit=2&offset=2
)
[top] => Array
(
[0] => stdClass Object
(
[viewers] => 86386
[channels] => 1159
[game] => stdClass Object
(
[name] => League of Legends
[_id] => 21779
[giantbomb_id] => 24024
[box] => stdClass Object
(
[template] => http://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-{width}x{height}.jpg
[small] => http://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-52x72.jpg
[medium] => http://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-136x190.jpg
[large] => http://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-272x380.jpg
)
[logo] => stdClass Object
(
[template] => http://static-cdn.jtvnw.net/ttv-logoart/League%20of%20Legends-{width}x{height}.jpg
[small] => http://static-cdn.jtvnw.net/ttv-logoart/League%20of%20Legends-60x36.jpg
[medium] => http://static-cdn.jtvnw.net/ttv-logoart/League%20of%20Legends-120x72.jpg
[large] => http://static-cdn.jtvnw.net/ttv-logoart/League%20of%20Legends-240x144.jpg
)
[_links] => stdClass Object
(
)
)
)
[1] => stdClass Object
(
[viewers] => 17288
[channels] => 162
[game] => stdClass Object
(
[name] => Hearthstone: Heroes of Warcraft
[_id] => 138585
[giantbomb_id] => 42033
[box] => stdClass Object
(
[template] => http://static-cdn.jtvnw.net/ttv-boxart/Hearthstone%3A%20Heroes%20of%20Warcraft-{width}x{height}.jpg
[small] => http://static-cdn.jtvnw.net/ttv-boxart/Hearthstone%3A%20Heroes%20of%20Warcraft-52x72.jpg
[medium] => http://static-cdn.jtvnw.net/ttv-boxart/Hearthstone%3A%20Heroes%20of%20Warcraft-136x190.jpg
[large] => http://static-cdn.jtvnw.net/ttv-boxart/Hearthstone%3A%20Heroes%20of%20Warcraft-272x380.jpg
)
[logo] => stdClass Object
(
[template] => http://static-cdn.jtvnw.net/ttv-logoart/Hearthstone%3A%20Heroes%20of%20Warcraft-{width}x{height}.jpg
[small] => http://static-cdn.jtvnw.net/ttv-logoart/Hearthstone%3A%20Heroes%20of%20Warcraft-60x36.jpg
[medium] => http://static-cdn.jtvnw.net/ttv-logoart/Hearthstone%3A%20Heroes%20of%20Warcraft-120x72.jpg
[large] => http://static-cdn.jtvnw.net/ttv-logoart/Hearthstone%3A%20Heroes%20of%20Warcraft-240x144.jpg
)
[_links] => stdClass Object
(
)
)
)
)
)
I can access single line (not sure if this is best practise either):
$OBJ->method()->top[0]->game->name;
But i'm more than clueless how to loop through all game names.
Any help much appreciated!
The "name"s are accessed using $OBJ->top[0]->game->name etc... So just foreach over the "top" array:
foreach($OBJ->top as $object) {
echo $object->game->name;
}
Create an empty array, loop the objects top array and fill your empty array:
$allgames=array();
foreach($OBJ->method()->top as $ob){
$allgames[] = $ob->game->name;
}
When you load your JSON string into PHP you can use:
json_decode($string_of_json, true);
The true flag will load it into an array you can loop through using, for example, foreach.
I am using the php library from here and I have a small problem.
To get some info for a invoice 118868, I can do this
<?php
$invoice_id=118868;
$invoice=$freshbooks->invoiceGet($invoice_id);
print_r($invoice);
?>
This is the output for print_r
Class Object ( [#attributes] => stdClass Object ( [status] => ok ) [invoice] => stdClass Object ( [invoice_id] => 00000219023 [estimate_id] => stdClass Object ( ) [number] => 8822 [client_id] => 83 [contacts] => stdClass Object ( [contact] => stdClass Object ( [contact_id] => 0 ) ) [recurring_id] => stdClass Object ( ) [organization] => Jimmy Thwart [first_name] => stdClass Object ( ) [last_name] => stdClass Object ( ) [p_street1] => stdClass Object ( ) [p_street2] => stdClass Object ( ) [p_city] => stdClass Object ( ) [p_state] => stdClass Object ( ) [p_country] => stdClass Object ( ) [p_code] => stdClass Object ( ) [po_number] => 10002 [status] => sent [amount] => 16.90 [amount_outstanding] => 16.90 [paid] => 0.00 [date] => 2013-01-31 00:00:00 [notes] => stdClass Object ( ) [terms] => Your slot can only be secured upon payment. Any reservation made before payment will only be guaranteed for 2 days. [discount] => 0 [url] => https://example.freshbooks.com/view/vgPb2TNGCR7n8JV [auth_url] => https://example.freshbooks.com/invoices/219023 [links] => stdClass Object ( [client_view] => https://example.freshbooks.com/view/vgPb2TNGCR7n8JV [view] => https://example.freshbooks.com/invoices/219023 [edit] => https://example.freshbooks.com/invoices/219023/edit ) [return_uri] => stdClass Object ( ) [updated] => 2013-01-31 02:25:13 [currency_code] => SGD [language] => en [vat_name] => stdClass Object ( ) [vat_number] => stdClass Object ( ) [folder] => active [staff_id] => 1 [lines] => stdClass Object ( [line] => stdClass Object ( [line_id] => 1 [name] => Lady 1pax [description] => Services (1 pax) [unit_cost] => 16.90 [quantity] => 1 [amount] => 16.90 [tax1_name] => stdClass Object ( ) [tax2_name] => stdClass Object ( ) [tax1_percent] => 0 [tax2_percent] => 0 [compound_tax] => 0 [type] => Item ) ) [gateways] => stdClass Object ( [gateway] => stdClass Object ( [name] => PayPal ) ) ) )
I hope the output can only be the URL and not this whole chunk of code.
Output wanted:
https://example.freshbooks.com/view/vgPb2TNGCR7n8JV
However, it lists all information of the invoice. How do I get only the invoice URL?
Well, using the output of your code, you can see what the elements of $invoice are. Use the one that you need.
Invoice is an instane of a class. You should treat it like all instances.
To get URL value you can do the next:
<?php
$invoice_id=118868;
$invoice=$freshbooks->invoiceGet($invoice_id);
print $invoice->url;
?>
or there's another possible value you're looking for:
<?php
$invoice_id=118868;
$invoice=$freshbooks->invoiceGet($invoice_id);
print $invoice->links->client_view;
?>
According to official API doc <url> is not supporting any more, so you should use <links> "element to provide your customers with direct links to the invoice for editing, viewing by the client and viewing by an administrator."
This seems like such a rookie question but I'm just banging my head against the keyboard here and I can't find anything answered already that gets me moving forward.
Scenario is I'm trying to get the Lat/Lng of a zip code by geocoding it with Google Maps API. I've gotten the results back from Google Maps API as a JSON string and I've used json_decode to put it into a PHP array. But it looks likt it's an array of objects and i'm stumped on how I can drill down into the data to get my lat/lng value.
Here is the current road block... code then results:
<?php
$jsonData = '{"results":[{"address_components":[{"long_name":"33647","short_name":"33647","types":["postal_code"]},{"long_name":"Tampa","short_name":"Tampa","types":["locality","political"]},{"long_name":"Florida","short_name":"FL","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]}],"formatted_address":"Tampa, FL 33647, USA","geometry":{"bounds":{"northeast":{"lat":28.17150,"lng":-82.26235779999999},"southwest":{"lat":28.07291710,"lng":-82.42569910}},"location":{"lat":28.14343180,"lng":-82.33433749999999},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":28.17150,"lng":-82.26235779999999},"southwest":{"lat":28.07291710,"lng":-82.42569910}}},"types":["postal_code"]}],"status":"OK"}';
$phpArray = json_decode($jsonData);
print_r($phpArray);
foreach ($phpArray as $key => $value) {
echo "<p>$key | $value</p>";
}
?>
Results:
stdClass Object ( [results] => Array ( [0] => stdClass Object ( [address_components] => Array ( [0] => stdClass Object ( [long_name] => 33647 [short_name] => 33647 [types] => Array ( [0] => postal_code ) ) [1] => stdClass Object ( [long_name] => Tampa [short_name] => Tampa [types] => Array ( [0] => locality [1] => political ) ) [2] => stdClass Object ( [long_name] => Florida [short_name] => FL [types] => Array ( [0] => administrative_area_level_1 [1] => political ) ) [3] => stdClass Object ( [long_name] => United States [short_name] => US [types] => Array ( [0] => country [1] => political ) ) ) [formatted_address] => Tampa, FL 33647, USA [geometry] => stdClass Object ( [bounds] => stdClass Object ( [northeast] => stdClass Object ( [lat] => 28.1715 [lng] => -82.2623578 ) [southwest] => stdClass Object ( [lat] => 28.0729171 [lng] => -82.4256991 ) ) [location] => stdClass Object ( [lat] => 28.1434318 [lng] => -82.3343375 ) [location_type] => APPROXIMATE [viewport] => stdClass Object ( [northeast] => stdClass Object ( [lat] => 28.1715 [lng] => -82.2623578 ) [southwest] => stdClass Object ( [lat] => 28.0729171 [lng] => -82.4256991 ) ) ) [types] => Array ( [0] => postal_code ) ) ) [status] => OK )
results | Array
status | OK
URL used to create the input JSON:
http://maps.googleapis.com/maps/api/geocode/json?address=33647&sensor=false
Looking for some help to pull out the Lat and Long values out into a PHP variable.
Thanks in advance!
Josh
The value of results is in fact another array - so you need to dig into the array to get the values you need.
This page (http://json.parser.online.fr/) might help you to visualize the data a bit more clearly.
Here's a terrible example with your data to demonstrate the depth (arrays as values):
<?php
$jsonData = '{"results":[{"address_components":[{"long_name":"33647","short_name":"33647","types":["postal_code"]},{"long_name":"Tampa","short_name":"Tampa","types":["locality","political"]},{"long_name":"Florida","short_name":"FL","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]}],"formatted_address":"Tampa, FL 33647, USA","geometry":{"bounds":{"northeast":{"lat":28.17150,"lng":-82.26235779999999},"southwest":{"lat":28.07291710,"lng":-82.42569910}},"location":{"lat":28.14343180,"lng":-82.33433749999999},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":28.17150,"lng":-82.26235779999999},"southwest":{"lat":28.07291710,"lng":-82.42569910}}},"types":["postal_code"]}],"status":"OK"}';
$phpArray = json_decode($jsonData,true);
print_r($phpArray);
foreach ($phpArray as $key => $value) {
if ( $key == "results") {
foreach ($value as $key2 => $value2) {
foreach ($value2 as $key3 => $value3) {
echo "<p>$key3 | $value3</p>";
}
}
}
}
?>
You'll need to dig down a couple of levels to find the ll data you want. This should point you in the right direction though.