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
)
)
)
Related
Below the array i have to format array according to their name which is in [c] index
// $name = $array[result][0][_id][c];
i want to sum data regarding their name (ex:Maharashtra & Goa) with their pl value
Array
(
[result] => Array
(
[0] => Array
(
[_id] => Array
(
[c] => Maharashtra & Goa
[d] => MongoDate Object
(
[sec] => 1412746200
[usec] => 0
)
)
[pl] => 107369655
)
[1] => Array
(
[_id] => Array
(
[c] => unknown
[d] => MongoDate Object
(
[sec] => 1412659800
[usec] => 0
)
)
[pl] => 69356849
)
[2] => Array
(
[_id] => Array
(
[c] => Maharashtra & Goa
[d] => MongoDate Object
(
[sec] => 1412832600
[usec] => 0
)
)
[pl] => 22320916
)
[3] => Array
(
[_id] => Array
(
[c] => unknown
[d] => MongoDate Object
(
[sec] => 1412746200
[usec] => 0
)
)
[pl] => 8397870
)
[4] => Array
(
[_id] => Array
(
[c] => Bihar & Jharkhand
[d] => MongoDate Object
(
[sec] => 1412746200
[usec] => 0
)
)
[pl] => 4278330
)
[5] => Array
(
[_id] => Array
(
[c] => unknown
[d] => MongoDate Object
(
[sec] => 1412832600
[usec] => 0
)
)
[pl] => 4183450
)
[6] => Array
(
[_id] => Array
(
[c] => Tamil Nadu
[d] => MongoDate Object
(
[sec] => 1412832600
[usec] => 0
)
)
[pl] => 4113634
)
[7] => Array
(
[_id] => Array
(
[c] => Maharashtra & Goa
[d] => MongoDate Object
(
[sec] => 1412487000
[usec] => 0
)
)
[pl] => 3716731
)
[8] => Array
(
[_id] => Array
(
[c] => Maharashtra & Goa
[d] => MongoDate Object
(
[sec] => 1412659800
[usec] => 0
)
)
[pl] => 1203054
)
[9] => Array
(
[_id] => Array
(
[c] => unknown
[d] => MongoDate Object
(
[sec] => 1412573400
[usec] => 0
)
)
[pl] => 554806
)
)
[ok] => 1
)
// Desired array
// pl will be sum of their name
Array (
[0] => Array
(
[name] => Maharashtra & Goa
[pl] => 134610356
)
[1] => Array
(
[name] => unknown
[pl] => 82492975
)
[2] => Array
(
[name] => Bihar & Jharkhand
[pl] => 4278330
)
[3] => Array
(
[name] => Tamil Nadu
[pl] => 4113634
)
)
You could just use that name index to group those values and finally just add them.
Example:
foreach ($array['result'] as $value) {
if(!isset($resultData[$value['_id']['c']])) { // initialize
$resultData[$value['_id']['c']] = array('name' => $value['_id']['c'], 'pl' => 0);
}
$resultData[$value['_id']['c']]['pl'] += $value['pl']; // sum them
}
$resultData = array_values($resultData); // simple reindex
echo '<pre>';
print_r($resultData);
// i tried this
$resultData =array();
foreach($array['result'] as $key=>$val){
$itemKey = str_replace(' ', '_', $val['_id']['c']);
//$resultData[$itemKey]= $resultData[$itemKey]+$val['pl'];
if(isset($resultData[$itemKey])){
$resultData[$itemKey][] =array('name'=>$val['_id']['c'],'p1'=>$val['pl']);
$resultData[$itemKey]['total'] +=$val['pl'];
// $resultData[$itemKey][$i]['pl']+= $val['pl'];
}else{
$resultData[$itemKey][] =array('name'=>$val['_id']['c'],'p1'=>$val['pl']);
$resultData[$itemKey]['total'] =$val['pl'];
}
}
// it produce result
Array
(
[Maharashtra_&_Goa] => Array
(
[0] => Array
(
[name] => Maharashtra & Goa
[p1] => 107369655
)
[total] => 134610356
[1] => Array
(
[name] => Maharashtra & Goa
[p1] => 22320916
)
[2] => Array
(
[name] => Maharashtra & Goa
[p1] => 3716731
)
[3] => Array
(
[name] => Maharashtra & Goa
[p1] => 1203054
)
)
[unknown] => Array
(
[0] => Array
(
[name] => unknown
[p1] => 69356849
)
[total] => 82492975
[1] => Array
(
[name] => unknown
[p1] => 8397870
)
[2] => Array
(
[name] => unknown
[p1] => 4183450
)
[3] => Array
(
[name] => unknown
[p1] => 554806
)
)
[Bihar_&_Jharkhand] => Array
(
[0] => Array
(
[name] => Bihar & Jharkhand
[p1] => 4278330
)
[total] => 4278330
)
[Tamil_Nadu] => Array
(
[0] => Array
(
[name] => Tamil Nadu
[p1] => 4113634
)
[total] => 4113634
)
)
foreach($aggregateResult['result'] as $key=>$val){
$itemKey = str_replace(' ', '_', $val['_id']['c']);
$resultData[$itemKey]= $resultData[$itemKey]+$val['pl'];
}
//will produce in key value
Array
(
[unknown] => 82492975
[Maharashtra_&_Goa] => 134610356
[Bihar_&_Jharkhand] => 4278330
[Tamil Nadu] => 4113634
)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is my array
Array
(
[TrackResponse] => Array
(
[Response] => Array
(
[TransactionReference] => Array
(
[CustomerContext] => RocketShipIt
)
[ResponseStatusCode] => 1
[ResponseStatusDescription] => Success
)
[Shipment] => Array
(
[Shipper] => Array
(
[ShipperNumber] => 746354
[Address] => Array
(
[AddressLine1] => 11 PINE ST
[City] => NEW BEDFORD
[StateProvinceCode] => MA
[PostalCode] => 01032 9785
[CountryCode] => US
)
)
[ShipTo] => Array
(
[Address] => Array
(
[City] => FOUNTAIN HILLS
[StateProvinceCode] => AZ
[PostalCode] => 85268
[CountryCode] => US
)
)
[ShipmentWeight] => Array
(
[UnitOfMeasurement] => Array
(
[Code] => LBS
)
[Weight] => 1.00
)
[Service] => Array
(
[Code] => 003
[Description] => GROUND
)
[ShipmentIdentificationNumber] => 746463772264354327
[PickupDate] => 20140709
[DeliveryDateUnavailable] => Array
(
[Type] => Scheduled Delivery
[Description] => Scheduled Delivery Date is not currently available, please try back later
)
[Package] => Array
(
[TrackingNumber] => 746463772264354327
[Activity] => Array
(
[0] => Array
(
[ActivityLocation] => Array
(
[Address] => Array
(
[City] => FOUNTAIN HILLS
[StateProvinceCode] => AZ
[PostalCode] => 85268
[CountryCode] => US
)
[Code] => ML
[Description] => FRONT DOOR
)
[Status] => Array
(
[StatusType] => Array
(
[Code] => D
[Description] => DELIVERED
)
[StatusCode] => Array
(
[Code] => FS
)
)
[Date] => 20140716
[Time] => 142400
)
[1] => Array
(
[ActivityLocation] => Array
(
[Address] => Array
(
[City] => TEMPE
[StateProvinceCode] => AZ
[CountryCode] => US
)
)
[Status] => Array
(
[StatusType] => Array
(
[Code] => I
[Description] => OUT FOR DELIVERY
)
[StatusCode] => Array
(
[Code] => DS
)
)
[Date] => 20140716
[Time] => 041900
)
[2] => Array
(
[ActivityLocation] => Array
(
[Address] => Array
(
[City] => TEMPE
[StateProvinceCode] => AZ
[CountryCode] => US
)
)
[Status] => Array
(
[StatusType] => Array
(
[Code] => I
[Description] => ARRIVAL SCAN
)
[StatusCode] => Array
(
[Code] => AR
)
)
[Date] => 20140715
[Time] => 114500
)
[3] => Array
(
[ActivityLocation] => Array
(
[Address] => Array
(
[City] => HODGKINS
[StateProvinceCode] => IL
[CountryCode] => US
)
)
[Status] => Array
(
[StatusType] => Array
(
[Code] => I
[Description] => DEPARTURE SCAN
)
[StatusCode] => Array
(
[Code] => DP
)
)
[Date] => 20140711
[Time] => 144600
)
[4] => Array
(
[ActivityLocation] => Array
(
[Address] => Array
(
[City] => HODGKINS
[StateProvinceCode] => IL
[CountryCode] => US
)
)
[Status] => Array
(
[StatusType] => Array
(
[Code] => I
[Description] => LOCATION SCAN
)
[StatusCode] => Array
(
[Code] => LC
)
)
[Date] => 20140711
[Time] => 090500
)
[5] => Array
(
[ActivityLocation] => Array
(
[Address] => Array
(
[City] => HODGKINS
[StateProvinceCode] => IL
[CountryCode] => US
)
)
[Status] => Array
(
[StatusType] => Array
(
[Code] => I
[Description] => UNLOAD SCAN
)
[StatusCode] => Array
(
[Code] => UL
)
)
[Date] => 20140711
[Time] => 085300
)
[6] => Array
(
[ActivityLocation] => Array
(
[Address] => Array
(
[City] => HODGKINS
[StateProvinceCode] => IL
[CountryCode] => US
)
)
[Status] => Array
(
[StatusType] => Array
(
[Code] => I
[Description] => ARRIVAL SCAN
)
[StatusCode] => Array
(
[Code] => AR
)
)
[Date] => 20140711
[Time] => 061900
)
[7] => Array
(
[ActivityLocation] => Array
(
[Address] => Array
(
[City] => W SPRINGFIELD
[StateProvinceCode] => MA
[CountryCode] => US
)
)
[Status] => Array
(
[StatusType] => Array
(
[Code] => I
[Description] => DEPARTURE SCAN
)
[StatusCode] => Array
(
[Code] => DP
)
)
[Date] => 20140709
[Time] => 213400
)
[8] => Array
(
[ActivityLocation] => Array
(
[Address] => Array
(
[City] => W SPRINGFIELD
[StateProvinceCode] => MA
[CountryCode] => US
)
)
[Status] => Array
(
[StatusType] => Array
(
[Code] => I
[Description] => ORIGIN SCAN
)
[StatusCode] => Array
(
[Code] => OR
)
)
[Date] => 20140709
[Time] => 183400
)
[9] => Array
(
[ActivityLocation] => Array
(
[Address] => Array
(
[City] => W SPRINGFIELD
[StateProvinceCode] => MA
[CountryCode] => US
)
)
[Status] => Array
(
[StatusType] => Array
(
[Code] => P
[Description] => PICKUP SCAN
)
[StatusCode] => Array
(
[Code] => PU
)
)
[Date] => 20140709
[Time] => 163200
)
[10] => Array
(
[ActivityLocation] => Array
(
[Address] => Array
(
[CountryCode] => US
)
)
[Status] => Array
(
[StatusType] => Array
(
[Code] => M
[Description] => BILLING INFORMATION RECEIVED
)
[StatusCode] => Array
(
[Code] => MP
)
)
[Date] => 20140709
[Time] => 112842
)
)
[PackageWeight] => Array
(
[UnitOfMeasurement] => Array
(
[Code] => LBS
)
[Weight] => 1.00
)
)
)
)
)
I'm looking to access values in the Activity numeric indexed array like such.
foreach($response as $row)
{
foreach($row['Shipment']['Package'] as $k)
{
echo $k['ActivityLocation']['Address']['City'];
}
}
I wanna be able to access the values inside of the numeric keyed array, and print them out in a row 1-10 etc.
The error i'm getting is Fatal error: Cannot use string offset as an array in test.php on line 287
If anybody could help me solve this you would be the bestus!
Please and Thanks
foreach($row['Shipment']['Package'] as $k)
{
echo $k['ActivityLocation']['Address']['City'];
}
$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
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Able to see a variable in print_r()'s output, but not sure how to access it in code
I've got a complex array variable called $data. I need to pull out one very buried value: [url] in [field_website]. Here's the raw print_r() output of the $data variable:
stdClass Object ( [node_title] => CMI2 [nid] => 3 [field_data_field_website_node_entity_type] => node [field_data_field_blog_node_entity_type] => node [field_data_field_rss_node_entity_type] => node [field_data_field_twitter_node_entity_type] => node [field_data_field_yammer_node_entity_type] => node [field_data_field_facebook_node_entity_type] => node [field_data_field_flickr_node_entity_type] => node [field_data_field_youtube_node_entity_type] => node [_field_data] => Array ( [nid] => Array ( [entity_type] => node [entity] => stdClass Object ( [vid] => 3 [uid] => 1 [title] => CMI2 [log] => [status] => 1 [comment] => 1 [promote] => 0 [sticky] => 0 [nid] => 3 [type] => social_source [language] => und [created] => 1356040541 [changed] => 1356040541 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1356040541 [revision_uid] => 1 [field_website] => Array ( [und] => Array ( [0] => Array ( [url] => http://cmi2.yale.edu [title] => [attributes] => Array ( ) ) ) ) [field_blog] => Array ( ) [field_rss] => Array ( ) [field_twitter] => Array ( [und] => Array ( [0] => Array ( [url] => http://twitter.com/yalecmi2 [title] => [attributes] => Array ( ) ) ) ) [field_facebook] => Array ( ) [field_youtube] => Array ( ) [field_flickr] => Array ( ) [field_yammer] => Array ( ) [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] => 1356040541 [last_comment_name] => [last_comment_uid] => 1 [comment_count] => 0 [name] => admin [picture] => 0 [data] => b:0; ) ) ) [field_field_website] => Array ( [0] => Array ( [rendered] => Array ( [#markup] => http://cmi2.yale.edu [#access] => 1 ) [raw] => Array ( [url] => http://cmi2.yale.edu [title] => http://cmi2.yale.edu [attributes] => Array ( ) [display_url] => http://cmi2.yale.edu ) ) ) [field_field_blog] => Array ( ) [field_field_rss] => Array ( ) [field_field_twitter] => Array ( [0] => Array ( [rendered] => Array ( [#markup] => http://twitter.com/yalecmi2 [#access] => 1 ) [raw] => Array ( [url] => http://twitter.com/yalecmi2 [title] => http://twitter.com/yalecmi2 [attributes] => Array ( ) [display_url] => http://twitter.com/yalecmi2 ) ) ) [field_field_yammer] => Array ( ) [field_field_facebook] => Array ( ) [field_field_flickr] => Array ( ) [field_field_youtube] => Array ( ) )
(sorry about that ugliness!)
How the heck do I pull out that [url] variable?! Ideally, I just want to assign that one value to another variable or just print it out.
If it's helpful, this is from a Drupal 7 view with the Views PHP module.
Thanks!
I recommend reading up about PHP arrays and objects since what you are trying to do is so trivial.
http://php.net/manual/en/language.types.array.php
http://php.net/manual/en/sdo.sample.getset.php