I am using PHP Simple HTML DOM Parser to gather some text from another website. When I use file_get_html($url) then I get the error, "Message: file_get_contents(http://www.yellowbook.com/profile/great-wall-restaurant_1855353442.html): failed to open stream: HTTP request failed! HTTP/1.1 416 Requested Range Not Satisfiable"
So I can use str_get_html($url) and I get a return but I can't find any elements afterward. Here is my code:
$data['yb_data_reviews'] = '';
$yb_reviews[] = '';
$yb_url = 'http://www.yellowbook.com/profile/great-wall-restaurant_1855353442.html';
if($yb_url){
$yb_dom = str_get_html($yb_url);
foreach($yb_dom->find('div.detailItem') as $yb_review) {
$item['author'] = $yb_review;
$yb_reviews[] = $item;
}
$data['yb_data_reviews'] = $yb_reviews;
}
print_r($data['yb_data_reviews']);
My return is blank. If I just return $yb_dom then I get:
simple_html_dom Object ( [root] => simple_html_dom_node Object ( [nodetype] => 5 [tag] => root [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( [0] => simple_html_dom_node Object ( [nodetype] => 3 [tag] => text [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( ) [parent] => simple_html_dom_node Object *RECURSION* [_] => Array ( [4] => http://www.yellowbook.com/profile/great-wall-restaurant_1855353442.html ) [tag_start] => 0 [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) ) [parent] => [_] => Array ( [0] => -1 [1] => 2 ) [tag_start] => 0 [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) [nodes] => Array ( [0] => simple_html_dom_node Object ( [nodetype] => 5 [tag] => root [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( [0] => simple_html_dom_node Object ( [nodetype] => 3 [tag] => text [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( ) [parent] => simple_html_dom_node Object *RECURSION* [_] => Array ( [4] => http://www.yellowbook.com/profile/great-wall-restaurant_1855353442.html ) [tag_start] => 0 [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) ) [parent] => [_] => Array ( [0] => -1 [1] => 2 ) [tag_start] => 0 [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) [1] => simple_html_dom_node Object ( [nodetype] => 3 [tag] => text [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( ) [parent] => simple_html_dom_node Object ( [nodetype] => 5 [tag] => root [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( [0] => simple_html_dom_node Object *RECURSION* ) [parent] => [_] => Array ( [0] => -1 [1] => 2 ) [tag_start] => 0 [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) [_] => Array ( [4] => http://www.yellowbook.com/profile/great-wall-restaurant_1855353442.html ) [tag_start] => 0 [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) ) [callback] => [lowercase] => 1 [original_size] => 71 [size] => 71 [pos:protected] => 71 [doc:protected] => http://www.yellowbook.com/profile/great-wall-restaurant_1855353442.html [char:protected] => [cursor:protected] => 2 [parent:protected] => simple_html_dom_node Object ( [nodetype] => 5 [tag] => root [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( [0] => simple_html_dom_node Object ( [nodetype] => 3 [tag] => text [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( ) [parent] => simple_html_dom_node Object *RECURSION* [_] => Array ( [4] => http://www.yellowbook.com/profile/great-wall-restaurant_1855353442.html ) [tag_start] => 0 [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) ) [parent] => [_] => Array ( [0] => -1 [1] => 2 ) [tag_start] => 0 [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) [noise:protected] => Array ( ) [token_blank:protected] => [token_equal:protected] => =/> [token_slash:protected] => /> [token_attr:protected] => > [_charset] => UTF-8 [_target_charset] => UTF-8 [default_br_text:protected] => [default_span_text] => [self_closing_tags:protected] => Array ( [img] => 1 [br] => 1 [input] => 1 [meta] => 1 [link] => 1 [hr] => 1 [base] => 1 [embed] => 1 [spacer] => 1 ) [block_tags:protected] => Array ( [root] => 1 [body] => 1 [form] => 1 [div] => 1 [span] => 1 [table] => 1 ) [optional_closing_tags:protected] => Array ( [tr] => Array ( [tr] => 1 [td] => 1 [th] => 1 ) [th] => Array ( [th] => 1 ) [td] => Array ( [td] => 1 ) [li] => Array ( [li] => 1 ) [dt] => Array ( [dt] => 1 [dd] => 1 ) [dd] => Array ( [dd] => 1 [dt] => 1 ) [dl] => Array ( [dd] => 1 [dt] => 1 ) [p] => Array ( [p] => 1 ) [nobr] => Array ( [nobr] => 1 ) [b] => Array ( [b] => 1 ) [option] => Array ( [option] => 1 ) ) )
What am I doing wrong that I can't return any elements using str_get_html()? Any help is appreciated. Thanks.
I have this array:
stdClass Object
(
[tid] => 26001835
[vid] => 5
[name] => AppleTV
[description] => My description
[format] => filtered_html
[weight] => 0
[vocabulary_machine_name] => how_to_watch_device
[field_device_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26608990
[alt] =>
[title] =>
[width] => 194
[height] => 102
[uid] => 26000697
[filename] => Apple-TV.png
[uri] => public://Apple-TV.png
[filemime] => image/png
[filesize] => 2103
[status] => 1
[timestamp] => 1405346182
)
)
)
[field_buy_now_button_link] => Array
(
[und] => Array
(
[0] => Array
(
[value] => http://www.something.com
[format] =>
[safe_value] => http://www.something.com
)
)
)
[field_learn_more] => Array
(
[und] => Array
(
[0] => Array
(
[value] => http://something.com/somepage
[format] =>
[safe_value] => http://something.com/somepage
)
)
)
[field_device_category] => Array
(
[und] => Array
(
[0] => Array
(
[value] => network
)
)
)
)
stdClass Object
(
[tid] => 26001834
[vid] => 5
[name] => Playstation - USA
[description] => My description
[format] => filtered_html
[weight] => 2
[vocabulary_machine_name] => how_to_watch_device
[field_device_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26608991
[alt] =>
[title] =>
[width] => 194
[height] => 102
[uid] => 26000697
[filename] => ps4network.png
[uri] => public://ps4network.png
[filemime] => image/png
[filesize] => 4566
[status] => 1
[timestamp] => 1405346218
)
)
)
[field_buy_now_button_link] => Array
(
[und] => Array
(
[0] => Array
(
[value] => http://www.somesite.com
[format] =>
[safe_value] => http://somesite.com
)
)
)
[field_learn_more] => Array
(
)
[field_device_category] => Array
(
[und] => Array
(
[0] => Array
(
[value] => blast_areas
)
)
)
)
stdClass Object
(
[tid] => 26001836
[vid] => 5
[name] => Brighthouse Networks
[description] => My description
[format] => filtered_html
[weight] => 3
[vocabulary_machine_name] => how_to_watch_device
[field_device_image] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26608993
[alt] =>
[title] =>
[width] => 194
[height] => 102
[uid] => 26000697
[filename] => brighthouse.png
[uri] => public://brighthouse.png
[filemime] => image/png
[filesize] => 8392
[status] => 1
[timestamp] => 1405358781
)
)
)
[field_buy_now_button_link] => Array
(
)
[field_learn_more] => Array
(
)
[field_device_category] => Array
(
[und] => Array
(
[0] => Array
(
[value] => ppv_provider
)
)
)
)
I want to sort the array by the array by the value in field_device_category. Basically, I want to group the results but first I need to make sure all the objects are sorted by field_device_category.
Thanks in advance!
Use usort for this kind of sorting. It uses Quicksort in the background and takes a user-defined function to compare the array elements:
usort($array, "complicatedArrayComparer");
function complicatedArrayComparer($a,$b)
{
if ($a['field_device_category'] == $b['field_device_category']) {
return 0;
}
return ($a['field_device_category'] < $b['field_device_category']) ? -1 : 1;
}
$myArray = Array
(
[Header] => Array
(
[#attributes] => Array
(
[ShouldRecordPerformanceTime] => False
[Timestamp] => 2014-06-11 16:33:20:04501
[ReferenceID] => 8fc07483-94ff-4a70-a8fa-d54292598592
[RecentlyTime] => 2014-06-11 16:33:08
[AccessCount] => 30
[CurrentCount] => 2
[ResetTime] => 2014-06-11 16:34:08
[ResultCode] => Success
)
)
[HotelResponse] => Array
(
[OTA_HotelSearchRS] => Array
(
[#attributes] => Array
(
[TimeStamp] => 2014-06-11T16:33:19.8857903+08:00
[Version] => 1.0
[PrimaryLangID] => zh
)
[Properties] => Array
(
[Property] => Array
(
[#attributes] => Array
(
[BrandCode] => 110
[HotelCode] => 443707
[HotelCityCode] => 2
[HotelName] => rujiakuaijie
[AreaID] => 112
[HotelId] => 443707
)
[VendorMessages] => Array
(
)
[Position] => Array
(
[#attributes] => Array
(
[Latitude] => 31.235452
[Longitude] => 121.447776
[PositionTypeCode] => 502
)
)
[Address] => Array
(
[AddressLine] => changpin road
[CityName] => SHANGHAI
[PostalCode] => 200040
)
[Award] => Array
(
[0] => Array
(
[#attributes] => Array
(
[Provider] => HotelStarRate
[Rating] => 2
)
)
[1] => Array
(
[#attributes] => Array
(
[Provider] => CtripStarRate
[Rating] => 2
)
)
[2] => Array
(
[#attributes] => Array
(
[Provider] => CtripRecommendRate
[Rating] => 1.5
)
)
[3] => Array
(
[#attributes] => Array
(
[Provider] => CtripCommRate
[Rating] => 0
)
)
)
[RelativePosition] => Array
(
[0] => Array
(
[#attributes] => Array
(
[Distance] => 1.58
[UnitOfMeasureCode] => 2
[Name] => changshou
)
)
[1] => Array
(
[#attributes] => Array
(
[Distance] => 44.729
[UnitOfMeasureCode] => 2
[Name] => pudong
)
)
[2] => Array
(
[#attributes] => Array
(
[Distance] => 12.88
[UnitOfMeasureCode] => 2
[Name] => hongqiao
)
)
[3] => Array
(
[#attributes] => Array
(
[Distance] => 11.208
[UnitOfMeasureCode] => 2
[Name] => shanghai
)
)
[4] => Array
(
[#attributes] => Array
(
[Distance] => 15.571
[UnitOfMeasureCode] => 2
[Name] => trainstation
)
)
[5] => Array
(
[#attributes] => Array
(
[Distance] => 4.167
[UnitOfMeasureCode] => 2
[Name] => central
)
)
[6] => Array
(
[#attributes] => Array
(
[Distance] => 3.836
[UnitOfMeasureCode] => 2
[Name] => museum
)
)
)
[TPA_Extensions] => Array
(
[Zone] => Array
(
[ZoneType] => Array
(
[#attributes] => Array
(
[ZoneID] => 981
[ZoneName] => commercial
)
)
)
)
)
)
[Success] => Array
(
)
)
)
)
The array look like this ,I used the sample of php.net to loop through the array to get key and value
0 : a
0 : subA
1 : subB
0 : subsubA
1 : subsubB
0 : deepA
1 : deepB
unfortunately I got nothing response, anyone can help will be appaeciate
$iterator = new RecursiveArrayIterator($myArray);
iterator_apply($iterator, 'traverseStructure', array($iterator));
function traverseStructure($iterator) {
while ( $iterator -> valid() ) {
if ( $iterator -> hasChildren() ) {
traverseStructure($iterator -> getChildren());
}
else {
echo $iterator -> key() . ' : ' . $iterator -> current() .PHP_EOL;
}
$iterator -> next();
}
}
?>
function DFS($array) {
foreach ($array as $k => $v) {
if (is_array($v)) {
$this->DFS($v);
}
else echo '<br />'. $k. ' - '. $v;
}
}
I am trying to access data inside a php object. I am confused about what the exact stynax is. Here is the output of the object:
stdClass Object ( [vid] => 10 [uid] => 1 [title] => Beachhead Don [log] =>
[status] => 1 [comment] => 1 [promote] => 0 [sticky] => 0 [ds_switch] => [nid] =>
10 [type] => product [language] => und [created] => 1357668665 [changed] => 1358198386
[tnid] => 0 [translate] => 0 [revision_timestamp] => 1358198386 [revision_uid] => 1
[model] => B 3000 [list_price] => 0.00000 [cost] => 0.00000 [sell_price] => 1.00000
[weight] => 0 [weight_units] => lb [length] => 0 [width] => 0 [height] => 0
[length_units] => in [pkg_qty] => 1 [default_qty] => 1 [unique_hash] =>
2eec0fcc8483f3a3541870fb24223296 [ordering] => 0 [shippable] => 0 [price] => 1.00000
[body] => Array ( [und] => Array ( [0] => Array ( [value] => Description info
))))))))))))))))))))))))))))))))))))))0 [summary] => [format] => filtered_html
[safe_value] =>Description info ))))))))))))))))))))))))))))))))))))))0[safe_summary]
=> ) ) ) [uc_product_image] => Array ( [und] => Array ( [0] => Array ( [fid] => 11
[alt] => [title] => [width] => 90 [height] => 120 [uid] => 1 [filename] =>
beachhead_don.jpg [uri] => public://beachhead_don.jpg [filemime] => image/jpeg
[filesize] => 14342 [status] => 1 [timestamp] => 1357668665 [rdf_mapping] => Array ( )
) ) ) [field_author] => Array ( [und] => Array ( [0] => Array ( [value] => John B.
Romesier [format] => [safe_value] => John B. Romesier ) ) ) [field_publisher] => Array
( [und] => Array ( [0] => Array ( [value] => Unknown [format] => [safe_value] =>
Unknown ) ) ) [field_release_date] => Array ( [und] => Array ( [0] => Array ( [value]
=> Mar 2012 [format] => [safe_value] => Mar 2012 ) ) ) [field_number_of_pages] => Array
( [und] => Array ( [0] => Array ( [value] => 123 ) ) ) [field_catagory] => Array (
[und] => Array ( [0] => Array ( [tid] => 4 ) ) ) [field_book_type] => Array ( [und] => Array ( [0] => Array ( [tid] => 1 ) ) ) [field_isbn] => Array ( ) [field_about_the_author] => Array ( [und] => Array ( [0] => Array ( [value] => Beachhed Don authoer............................ [format] => [safe_value] => Beachhed Don authoer............................ ) ) ) [rdf_mapping] => Array ( [rdftype] => Array ( [0] => sioc:Item [1] => foaf:Document ) [title] => Array ( [predicates] => Array ( [0] => dc:title ) ) [created] => Array ( [predicates] => Array ( [0] => dc:date [1] => dc:created ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [changed] => Array ( [predicates] => Array ( [0] => dc:modified ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [body] => Array ( [predicates] => Array ( [0] => content:encoded ) ) [uid] => Array ( [predicates] => Array ( [0] => sioc:has_creator ) [type] => rel ) [name] => Array ( [predicates] => Array ( [0] => foaf:name ) ) [comment_count] => Array ( [predicates] => Array ( [0] => sioc:num_replies ) [datatype] => xsd:integer ) [last_activity] => Array ( [predicates] => Array ( [0] => sioc:last_activity_date ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) ) [cid] => 0 [last_comment_timestamp] => 1357668665 [last_comment_name] => [last_comment_uid] => 1 [comment_count] => 0 [name] => admin [picture] => 0 [data] => b:0; [entity_view_prepared] => 1 )
I am trying to retrive the [value] of Description info ))))))))))))))))))))))))))))))))))))))0
I tried this
<?php
$node = menu_get_object(); //drupal code
if ( !empty($node) ) {
print $node ->body=>und=>0=>value;
}?>
and got an error about the '=' sign. What is the proper way to get my data?
[body] and [und] are arrays, so access them like this:
echo $node->body['und'][0]['value'];
It should be:
$node->body['und'][0]['value']
'Body' and 'und' are Arrays and need to be accessed with array syntax([]).
Accessing these values directly is sometimes frowned upon in drupal. You may want to look into: http://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_render/7
Below is the result of the array from which I want to grab only coordinates and store them in to the one dimensional array.
Array
(
[name] => jackson
[Status] => Array
(
[code] => 200
[request] => geocode
)
[Placemark] => Array
(
[0] => Array
(
[#attributes] => Array
(
[id] => p1
)
[address] => Jackson, MS, USA
[AddressDetails] => Array
(
[#attributes] => Array
(
[Accuracy] => 4
)
[Country] => Array
(
[CountryNameCode] => US
[CountryName] => USA
[AdministrativeArea] => Array
(
[AdministrativeAreaName] => MS
[SubAdministrativeArea] => Array
(
[SubAdministrativeAreaName] => Hinds
[Locality] => Array
(
[LocalityName] => Jackson
)
)
)
)
)
[ExtendedData] => Array
(
[LatLonBox] => Array
(
[#attributes] => Array
(
[north] => 32.3741783
[south] => 32.2232735
[east] => -90.0567509
[west] => -90.3128697
)
)
)
[Point] => Array
(
[coordinates] => -90.1848103,32.2987573,0
)
)
[1] => Array
(
[#attributes] => Array
(
[id] => p2
)
[address] => Jackson, TN, USA
[AddressDetails] => Array
(
[#attributes] => Array
(
[Accuracy] => 4
)
[Country] => Array
(
[CountryNameCode] => US
[CountryName] => USA
[AdministrativeArea] => Array
(
[AdministrativeAreaName] => TN
[SubAdministrativeArea] => Array
(
[SubAdministrativeAreaName] => Madison
[Locality] => Array
(
[LocalityName] => Jackson
)
)
)
)
)
[ExtendedData] => Array
(
[LatLonBox] => Array
(
[#attributes] => Array
(
[north] => 35.7562880
[south] => 35.5402259
[east] => -88.7567579
[west] => -88.9204599
)
)
)
[Point] => Array
(
[coordinates] => -88.8139469,35.6145169,0
)
)
[2] => Array
(
[#attributes] => Array
(
[id] => p3
)
[address] => Jackson, WY, USA
[AddressDetails] => Array
(
[#attributes] => Array
(
[Accuracy] => 4
)
[Country] => Array
(
[CountryNameCode] => US
[CountryName] => USA
[AdministrativeArea] => Array
(
[AdministrativeAreaName] => WY
[SubAdministrativeArea] => Array
(
[SubAdministrativeAreaName] => Teton
[Locality] => Array
(
[LocalityName] => Jackson
)
)
)
)
)
[ExtendedData] => Array
(
[LatLonBox] => Array
(
[#attributes] => Array
(
[north] => 43.4912050
[south] => 43.4578330
[east] => -110.7377220
[west] => -110.8134730
)
)
)
[Point] => Array
(
[coordinates] => -110.7624282,43.4799291,0
)
)
[3] => Array
(
[#attributes] => Array
(
[id] => p4
)
[address] => Jackson, NJ, USA
[AddressDetails] => Array
(
[#attributes] => Array
(
[Accuracy] => 4
)
[Country] => Array
(
[CountryNameCode] => US
[CountryName] => USA
[AdministrativeArea] => Array
(
[AdministrativeAreaName] => NJ
[SubAdministrativeArea] => Array
(
[SubAdministrativeAreaName] => Ocean
[Locality] => Array
(
[LocalityName] => Jackson
)
)
)
)
)
[ExtendedData] => Array
(
[LatLonBox] => Array
(
[#attributes] => Array
(
[north] => 40.1723549
[south] => 39.9990330
[east] => -74.2415390
[west] => -74.4695430
)
)
)
[Point] => Array
(
[coordinates] => -74.3294444,40.1080556,0
)
)
[4] => Array
(
[#attributes] => Array
(
[id] => p5
)
[address] => Jackson, MI, USA
[AddressDetails] => Array
(
[#attributes] => Array
(
[Accuracy] => 4
)
[Country] => Array
(
[CountryNameCode] => US
[CountryName] => USA
[AdministrativeArea] => Array
(
[AdministrativeAreaName] => MI
[SubAdministrativeArea] => Array
(
[SubAdministrativeAreaName] => Jackson
[Locality] => Array
(
[LocalityName] => Jackson
)
)
)
)
)
[ExtendedData] => Array
(
[LatLonBox] => Array
(
[#attributes] => Array
(
[north] => 42.2708699
[south] => 42.2036839
[east] => -84.3568169
[west] => -84.4345460
)
)
)
[Point] => Array
(
[coordinates] => -84.4013462,42.2458690,0
)
)
[5] => Array
(
[#attributes] => Array
(
[id] => p6
)
[address] => Jackson, WI, USA
[AddressDetails] => Array
(
[#attributes] => Array
(
[Accuracy] => 4
)
[Country] => Array
(
[CountryNameCode] => US
[CountryName] => USA
[AdministrativeArea] => Array
(
[AdministrativeAreaName] => WI
[SubAdministrativeArea] => Array
(
[SubAdministrativeAreaName] => Washington
[Locality] => Array
(
[LocalityName] => Jackson
)
)
)
)
)
[ExtendedData] => Array
(
[LatLonBox] => Array
(
[#attributes] => Array
(
[north] => 43.3387369
[south] => 43.3090429
[east] => -88.1422039
[west] => -88.1917450
)
)
)
[Point] => Array
(
[coordinates] => -88.1667599,43.3238919,0
)
)
[6] => Array
(
[#attributes] => Array
(
[id] => p7
)
[address] => Jackson, CA, USA
[AddressDetails] => Array
(
[#attributes] => Array
(
[Accuracy] => 4
)
[Country] => Array
(
[CountryNameCode] => US
[CountryName] => USA
[AdministrativeArea] => Array
(
[AdministrativeAreaName] => CA
[SubAdministrativeArea] => Array
(
[SubAdministrativeAreaName] => Amador
[Locality] => Array
(
[LocalityName] => Jackson
)
)
)
)
)
[ExtendedData] => Array
(
[LatLonBox] => Array
(
[#attributes] => Array
(
[north] => 38.3721550
[south] => 38.3302920
[east] => -120.7489930
[west] => -120.7981980
)
)
)
[Point] => Array
(
[coordinates] => -120.7741018,38.3488023,0
)
)
[7] => Array
(
[#attributes] => Array
(
[id] => p8
)
[address] => Jackson, GA, USA
[AddressDetails] => Array
(
[#attributes] => Array
(
[Accuracy] => 4
)
[Country] => Array
(
[CountryNameCode] => US
[CountryName] => USA
[AdministrativeArea] => Array
(
[AdministrativeAreaName] => GA
[SubAdministrativeArea] => Array
(
[SubAdministrativeAreaName] => Butts
[Locality] => Array
(
[LocalityName] => Jackson
)
)
)
)
)
[ExtendedData] => Array
(
[LatLonBox] => Array
(
[#attributes] => Array
(
[north] => 33.3114890
[south] => 33.2638330
[east] => -83.9354860
[west] => -84.0105969
)
)
)
[Point] => Array
(
[coordinates] => -83.9660209,33.2945651,0
)
)
[8] => Array
(
[#attributes] => Array
(
[id] => p9
)
[address] => Jackson, OH 45640, USA
[AddressDetails] => Array
(
[#attributes] => Array
(
[Accuracy] => 4
)
[Country] => Array
(
[CountryNameCode] => US
[CountryName] => USA
[AdministrativeArea] => Array
(
[AdministrativeAreaName] => OH
[SubAdministrativeArea] => Array
(
[SubAdministrativeAreaName] => Jackson
[Locality] => Array
(
[LocalityName] => Jackson
)
)
)
)
)
[ExtendedData] => Array
(
[LatLonBox] => Array
(
[#attributes] => Array
(
[north] => 39.0669040
[south] => 39.0062730
[east] => -82.5949490
[west] => -82.7069350
)
)
)
[Point] => Array
(
[coordinates] => -82.6365536,39.0520169,0
)
)
[9] => Array
(
[#attributes] => Array
(
[id] => p10
)
[address] => Jackson, MO, USA
[AddressDetails] => Array
(
[#attributes] => Array
(
[Accuracy] => 4
)
[Country] => Array
(
[CountryNameCode] => US
[CountryName] => USA
[AdministrativeArea] => Array
(
[AdministrativeAreaName] => MO
[SubAdministrativeArea] => Array
(
[SubAdministrativeAreaName] => Cape Girardeau
[Locality] => Array
(
[LocalityName] => Jackson
)
)
)
)
)
[ExtendedData] => Array
(
[LatLonBox] => Array
(
[#attributes] => Array
(
[north] => 37.4139659
[south] => 37.3385089
[east] => -89.5972280
[west] => -89.7035499
)
)
)
[Point] => Array
(
[coordinates] => -89.6662063,37.3822732,0
)
)
)
)
//data for single result starts here
Array
(
[Response] => Array
(
[name] => 10121
[Status] => Array
(
[code] => 200
[request] => geocode
)
[Placemark] => Array
(
[#attributes] => Array
(
[id] => p1
)
[address] => Manhattan, NY 10121, USA
[AddressDetails] => Array
(
[#attributes] => Array
(
[Accuracy] => 5
)
[Country] => Array
(
[CountryNameCode] => US
[CountryName] => USA
[AdministrativeArea] => Array
(
[AdministrativeAreaName] => NY
[DependentLocality] => Array
(
[DependentLocalityName] => Manhattan
[PostalCode] => Array
(
[PostalCodeNumber] => 10121
)
)
)
)
)
[ExtendedData] => Array
(
[LatLonBox] => Array
(
[#attributes] => Array
(
[north] => 40.7528519
[south] => 40.7489381
[east] => -73.9917906
[west] => -73.9947563
)
)
)
[Point] => Array
(
[coordinates] => -73.9917906,40.7492821,0
)
)
)
)
//data for single result ends here
I tried the following code for desired results but no success
foreach ($array as $xm) {
foreach ($xm as $points=>$pointkey) {
foreach($pointkey as $cor=>$corkey) {
echo $cor["coordinates"];
}
}
}
it gives me warning "Warning: Invalid argument supplied for foreach() in foreach($pointkey as $cor=>$corkey)"
Try something like:
$coords = array();
foreach ($data['Placemark'] as $entry) { // where $data holds the complete array
$coords[] = $entry['Point']['coordinates'];
}
var_dump($coords);
For only one result you can access the coordinates directly, like:
var_dump($data['Response']['Placemark']['Point']['coordinates']);
I would do something like this:
$coords = array();
array_walk_recursive($input_array, 'get_coords');
function get_coords($item, $key)
{
$coord = ($key === 'coordinates') ? $item : '';
if(!empty($coord))
{
$coords[] = $coord;
}
}
Array
(
[0] => Array
(
[#attributes] => Array
(
[request_id] => 0
[district] =>
[county] => WILTS
[ptc_abs_code] => 58150004231
[house_no] => 232
[post_town] => WESTBURY
[match_status] => 1
[house_name] =>
[postcode] => BA133BN
[surname] =>
[street_2] =>
[street_1] => HIGH ST
)
)
)