PHP - Looping through indexed JSON array - php

I have parsed and looped through JSON plenty of times. I have seemed to hit a wall with something I have not done before and I have been unable to find an answer and hopefully I am referencing this correctly. I have been really struggling with this.
With the code example below I can parse it and assign a varible to an object and get its value like so.
$result = json_decode($json, true);
print_r($result);
echo $result['response'][0]['report']['type'];
This is where the problem comes in and it starts with the array index and I am hoping I am referring to this correctly. Basically it is the [0] from above. I have never looped through something like that before and my research has come up empty. Normally I would do it like this.
$json = '{"success":true,"error":null,"response":[{"id":"59c30487db6be86b7f8b465a","loc":{"long":-90.45,"lat":43.43},"report":{"code":"R","type":"heavy rain","name":"Gillingham","detail":{"text":1,"rainIN":1,"rainMM":25.4},"reporter":"co-op observer","comments":"","timestamp":1505952240,"cat":"rain","dateTimeISO":"2017-09-20T19:04:00-05:00","datetime":"2017-09-20T19:04:00-05:00","wfo":"arx"},"place":{"name":"gillingham","state":"wi","county":"richland","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c302a9db6be82a758b46e8","loc":{"long":-90.93,"lat":43.32},"report":{"code":"R","type":"heavy rain","name":"Mount Sterling","detail":{"text":2.25,"rainIN":2.25,"rainMM":57.15},"reporter":"trained spotter","comments":"","timestamp":1505951760,"cat":"rain","dateTimeISO":"2017-09-20T18:56:00-05:00","datetime":"2017-09-20T18:56:00-05:00","wfo":"arx"},"place":{"name":"mount sterling","state":"wi","county":"crawford","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c30106db6be8146c8b4661","loc":{"long":-89.53,"lat":44.45},"report":{"code":"R","type":"heavy rain","name":"Plover","detail":{"text":1.05,"rainIN":1.05,"rainMM":26.67},"reporter":"co-op observer","comments":"One hour total rainfall. also measured a 49 mph wind gust at 511 pm. no hail.","timestamp":1505950800,"cat":"rain","dateTimeISO":"2017-09-20T18:40:00-05:00","datetime":"2017-09-20T18:40:00-05:00","wfo":"grb"},"place":{"name":"plover","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c3080adb6be8d5138b4654","loc":{"long":-89.33,"lat":44.37},"report":{"code":"H","type":"hail","name":"4 mi NNW Blaine","detail":{"text":0.88,"hailIN":0.88,"hailMM":22.35},"reporter":"trained spotter","comments":"Nickel size hail and heavy rain. 3.25 to 3.49 inches in the past 2 hours.","timestamp":1505950680,"cat":"hail","dateTimeISO":"2017-09-20T18:38:00-05:00","datetime":"2017-09-20T18:38:00-05:00","wfo":"grb"},"place":{"name":"blaine","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c30106db6be8146c8b4660","loc":{"long":-90.93,"lat":43.32},"report":{"code":"H","type":"hail","name":"Mount Sterling","detail":{"text":0.75,"hailIN":0.75,"hailMM":19.05},"reporter":"trained spotter","comments":"Hail ranged from 1\/2 to 3\/4 inch.","timestamp":1505950200,"cat":"hail","dateTimeISO":"2017-09-20T18:30:00-05:00","datetime":"2017-09-20T18:30:00-05:00","wfo":"arx"},"place":{"name":"mount sterling","state":"wi","county":"crawford","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2fd80db6be844598b466d","loc":{"long":-89.77,"lat":44.21},"report":{"code":"R","type":"heavy rain","name":"6 mi ESE New Rome","detail":{"text":4,"rainIN":4,"rainMM":101.6},"reporter":"trained spotter","comments":"","timestamp":1505949840,"cat":"rain","dateTimeISO":"2017-09-20T18:24:00-05:00","datetime":"2017-09-20T18:24:00-05:00","wfo":"arx"},"place":{"name":"rome","state":"wi","county":"adams","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2f678db6be898338b4673","loc":{"long":-89.3,"lat":44.46},"report":{"code":"H","type":"hail","name":"Amherst Junction","detail":{"text":1,"hailIN":1,"hailMM":25.4},"reporter":"trained spotter","comments":"","timestamp":1505948340,"cat":"hail","dateTimeISO":"2017-09-20T17:59:00-05:00","datetime":"2017-09-20T17:59:00-05:00","wfo":"grb"},"place":{"name":"amherst junction","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2f2f6db6be8a2208b4672","loc":{"long":-89.4,"lat":44.26},"report":{"code":"H","type":"hail","name":"Almond","detail":{"text":2,"hailIN":2,"hailMM":50.8},"reporter":"trained spotter","comments":"Report via social media","timestamp":1505948160,"cat":"hail","dateTimeISO":"2017-09-20T17:56:00-05:00","datetime":"2017-09-20T17:56:00-05:00","wfo":"grb"},"place":{"name":"almond","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}}]}';
$result = json_decode($json, true);
print_r($result);
foreach($result as report) {
$type = $report['report']['type'];
echo $type;
}
That would usually work in most cases but since this has the [0] and increases with each new entry I am not sure how to loop through that as my entry point is different that what I have dealt with before and have been unable to find any working examples for something like this. Not to mention I am not sure how to working ['response'] into the loop ether with those index keys.
Here is a decode example of the array and what I am working with.
Array ( [success] => 1 [error] => [response] => Array ( [0] => Array ( [id] => 59c30487db6be86b7f8b465a [loc] => Array ( [long] => -90.45 [lat] => 43.43 ) [report] => Array ( [code] => R [type] => heavy rain [name] => Gillingham [detail] => Array ( [text] => 1 [rainIN] => 1 [rainMM] => 25.4 ) [reporter] => co-op observer [comments] => [timestamp] => 1505952240 [cat] => rain [dateTimeISO] => 2017-09-20T19:04:00-05:00 [datetime] => 2017-09-20T19:04:00-05:00 [wfo] => arx ) [place] => Array ( [name] => gillingham [state] => wi [county] => richland [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) [1] => Array ( [id] => 59c302a9db6be82a758b46e8 [loc] => Array ( [long] => -90.93 [lat] => 43.32 ) [report] => Array ( [code] => R [type] => heavy rain [name] => Mount Sterling [detail] => Array ( [text] => 2.25 [rainIN] => 2.25 [rainMM] => 57.15 ) [reporter] => trained spotter [comments] => [timestamp] => 1505951760 [cat] => rain [dateTimeISO] => 2017-09-20T18:56:00-05:00 [datetime] => 2017-09-20T18:56:00-05:00 [wfo] => arx ) [place] => Array ( [name] => mount sterling [state] => wi [county] => crawford [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) [2] => Array ( [id] => 59c30106db6be8146c8b4661 [loc] => Array ( [long] => -89.53 [lat] => 44.45 ) [report] => Array ( [code] => R [type] => heavy rain [name] => Plover [detail] => Array ( [text] => 1.05 [rainIN] => 1.05 [rainMM] => 26.67 ) [reporter] => co-op observer [comments] => One hour total rainfall. also measured a 49 mph wind gust at 511 pm. no hail. [timestamp] => 1505950800 [cat] => rain [dateTimeISO] => 2017-09-20T18:40:00-05:00 [datetime] => 2017-09-20T18:40:00-05:00 [wfo] => grb ) [place] => Array ( [name] => plover [state] => wi [county] => portage [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) [3] => Array ( [id] => 59c3080adb6be8d5138b4654 [loc] => Array ( [long] => -89.33 [lat] => 44.37 ) [report] => Array ( [code] => H [type] => hail [name] => 4 mi NNW Blaine [detail] => Array ( [text] => 0.88 [hailIN] => 0.88 [hailMM] => 22.35 ) [reporter] => trained spotter [comments] => Nickel size hail and heavy rain. 3.25 to 3.49 inches in the past 2 hours. [timestamp] => 1505950680 [cat] => hail [dateTimeISO] => 2017-09-20T18:38:00-05:00 [datetime] => 2017-09-20T18:38:00-05:00 [wfo] => grb ) [place] => Array ( [name] => blaine [state] => wi [county] => portage [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) [4] => Array ( [id] => 59c30106db6be8146c8b4660 [loc] => Array ( [long] => -90.93 [lat] => 43.32 ) [report] => Array ( [code] => H [type] => hail [name] => Mount Sterling [detail] => Array ( [text] => 0.75 [hailIN] => 0.75 [hailMM] => 19.05 ) [reporter] => trained spotter [comments] => Hail ranged from 1/2 to 3/4 inch. [timestamp] => 1505950200 [cat] => hail [dateTimeISO] => 2017-09-20T18:30:00-05:00 [datetime] => 2017-09-20T18:30:00-05:00 [wfo] => arx ) [place] => Array ( [name] => mount sterling [state] => wi [county] => crawford [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) [5] => Array ( [id] => 59c2fd80db6be844598b466d [loc] => Array ( [long] => -89.77 [lat] => 44.21 ) [report] => Array ( [code] => R [type] => heavy rain [name] => 6 mi ESE New Rome [detail] => Array ( [text] => 4 [rainIN] => 4 [rainMM] => 101.6 ) [reporter] => trained spotter [comments] => [timestamp] => 1505949840 [cat] => rain [dateTimeISO] => 2017-09-20T18:24:00-05:00 [datetime] => 2017-09-20T18:24:00-05:00 [wfo] => arx ) [place] => Array ( [name] => rome [state] => wi [county] => adams [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) [6] => Array ( [id] => 59c2f678db6be898338b4673 [loc] => Array ( [long] => -89.3 [lat] => 44.46 ) [report] => Array ( [code] => H [type] => hail [name] => Amherst Junction [detail] => Array ( [text] => 1 [hailIN] => 1 [hailMM] => 25.4 ) [reporter] => trained spotter [comments] => [timestamp] => 1505948340 [cat] => hail [dateTimeISO] => 2017-09-20T17:59:00-05:00 [datetime] => 2017-09-20T17:59:00-05:00 [wfo] => grb ) [place] => Array ( [name] => amherst junction [state] => wi [county] => portage [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) [7] => Array ( [id] => 59c2f2f6db6be8a2208b4672 [loc] => Array ( [long] => -89.4 [lat] => 44.26 ) [report] => Array ( [code] => H [type] => hail [name] => Almond [detail] => Array ( [text] => 2 [hailIN] => 2 [hailMM] => 50.8 ) [reporter] => trained spotter [comments] => Report via social media [timestamp] => 1505948160 [cat] => hail [dateTimeISO] => 2017-09-20T17:56:00-05:00 [datetime] => 2017-09-20T17:56:00-05:00 [wfo] => grb ) [place] => Array ( [name] => almond [state] => wi [county] => portage [country] => us ) [profile] => Array ( [tz] => America/Chicago ) ) ) )
How would I loop through this with the Index key [#] and if I am referring to this incorrectly feel free to correct me.

You can use loop in $result["response"] as:
$json = '{"success":true,"error":null,"response":[{"id":"59c30487db6be86b7f8b465a","loc":{"long":-90.45,"lat":43.43},"report":{"code":"R","type":"heavy rain","name":"Gillingham","detail":{"text":1,"rainIN":1,"rainMM":25.4},"reporter":"co-op observer","comments":"","timestamp":1505952240,"cat":"rain","dateTimeISO":"2017-09-20T19:04:00-05:00","datetime":"2017-09-20T19:04:00-05:00","wfo":"arx"},"place":{"name":"gillingham","state":"wi","county":"richland","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c302a9db6be82a758b46e8","loc":{"long":-90.93,"lat":43.32},"report":{"code":"R","type":"heavy rain","name":"Mount Sterling","detail":{"text":2.25,"rainIN":2.25,"rainMM":57.15},"reporter":"trained spotter","comments":"","timestamp":1505951760,"cat":"rain","dateTimeISO":"2017-09-20T18:56:00-05:00","datetime":"2017-09-20T18:56:00-05:00","wfo":"arx"},"place":{"name":"mount sterling","state":"wi","county":"crawford","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c30106db6be8146c8b4661","loc":{"long":-89.53,"lat":44.45},"report":{"code":"R","type":"heavy rain","name":"Plover","detail":{"text":1.05,"rainIN":1.05,"rainMM":26.67},"reporter":"co-op observer","comments":"One hour total rainfall. also measured a 49 mph wind gust at 511 pm. no hail.","timestamp":1505950800,"cat":"rain","dateTimeISO":"2017-09-20T18:40:00-05:00","datetime":"2017-09-20T18:40:00-05:00","wfo":"grb"},"place":{"name":"plover","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c3080adb6be8d5138b4654","loc":{"long":-89.33,"lat":44.37},"report":{"code":"H","type":"hail","name":"4 mi NNW Blaine","detail":{"text":0.88,"hailIN":0.88,"hailMM":22.35},"reporter":"trained spotter","comments":"Nickel size hail and heavy rain. 3.25 to 3.49 inches in the past 2 hours.","timestamp":1505950680,"cat":"hail","dateTimeISO":"2017-09-20T18:38:00-05:00","datetime":"2017-09-20T18:38:00-05:00","wfo":"grb"},"place":{"name":"blaine","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c30106db6be8146c8b4660","loc":{"long":-90.93,"lat":43.32},"report":{"code":"H","type":"hail","name":"Mount Sterling","detail":{"text":0.75,"hailIN":0.75,"hailMM":19.05},"reporter":"trained spotter","comments":"Hail ranged from 1\/2 to 3\/4 inch.","timestamp":1505950200,"cat":"hail","dateTimeISO":"2017-09-20T18:30:00-05:00","datetime":"2017-09-20T18:30:00-05:00","wfo":"arx"},"place":{"name":"mount sterling","state":"wi","county":"crawford","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2fd80db6be844598b466d","loc":{"long":-89.77,"lat":44.21},"report":{"code":"R","type":"heavy rain","name":"6 mi ESE New Rome","detail":{"text":4,"rainIN":4,"rainMM":101.6},"reporter":"trained spotter","comments":"","timestamp":1505949840,"cat":"rain","dateTimeISO":"2017-09-20T18:24:00-05:00","datetime":"2017-09-20T18:24:00-05:00","wfo":"arx"},"place":{"name":"rome","state":"wi","county":"adams","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2f678db6be898338b4673","loc":{"long":-89.3,"lat":44.46},"report":{"code":"H","type":"hail","name":"Amherst Junction","detail":{"text":1,"hailIN":1,"hailMM":25.4},"reporter":"trained spotter","comments":"","timestamp":1505948340,"cat":"hail","dateTimeISO":"2017-09-20T17:59:00-05:00","datetime":"2017-09-20T17:59:00-05:00","wfo":"grb"},"place":{"name":"amherst junction","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}},{"id":"59c2f2f6db6be8a2208b4672","loc":{"long":-89.4,"lat":44.26},"report":{"code":"H","type":"hail","name":"Almond","detail":{"text":2,"hailIN":2,"hailMM":50.8},"reporter":"trained spotter","comments":"Report via social media","timestamp":1505948160,"cat":"hail","dateTimeISO":"2017-09-20T17:56:00-05:00","datetime":"2017-09-20T17:56:00-05:00","wfo":"grb"},"place":{"name":"almond","state":"wi","county":"portage","country":"us"},"profile":{"tz":"America\/Chicago"}}]}';
$result = json_decode($json, true);
foreach($result["response"] as $report) {
$type = $report['report']['type'];
echo $type;
}
Check the result here: http://sandbox.onlinephpfunctions.com/code/c7b55a67e649bdc8c35ec1ba8218747de1f8ac16

Related

How to insert a new values pair in an associative array in PHP?

I've an associative array. I am attempting to create each sub array within a loop and insert it into a multidimensional array. Google suggested using array_merge, but after using 'print_r' on the multidimensional array with the code below, only the last sub-array is being displayed.
Array
(
[data] => Array
(
[0] => Array
(
[candidate] => Array
(
[id] => 184
[firstName] => skg
[lastName] => s
[address] => Array
(
[address1] => sakthi
[address2] =>
[city] =>
[state] =>
[zip] =>
[countryID] => 1
[countryName] => United States
[countryCode] => US
)
[hourlyRate] => 0
)
[jobOrder] => Array
(
[id] => 88
[title] => Tech Analyst
)
)
[1] => Array
(
[candidate] => Array
(
[id] => 852
[firstName] => mso cool
[lastName] =>
[address] => Array
(
[address1] =>
[address2] =>
[city] =>
[state] =>
[zip] =>
[countryID] => 1
[countryName] => United States
[countryCode] => US
)
[hourlyRate] => 0
)
[jobOrder] => Array
(
[id] => 57
[title] => Tester
)
)
And Another Array like this :
[rating] => Array
(
[0] => 7
[1] => 5
[2] =>
)
How to get merge this array value into previous array values like
Array
(
[data] => Array
(
[0] => Array
(
[candidate] => Array
(
[id] => 184
[firstName] => skg
[lastName] => s
[address] => Array
(
[address1] => sakthi
[address2] =>
[city] =>
[state] =>
[zip] =>
[countryID] => 1
[countryName] => United States
[countryCode] => US
)
[hourlyRate] => 0
[rating] => 7
)
[jobOrder] => Array
(
[id] => 88
[title] => Tech Analyst
)
)
[1] => Array
(
[candidate] => Array
(
[id] => 852
[firstName] => mso cool
[lastName] =>
[address] => Array
(
[address1] =>
[address2] =>
[city] =>
[state] =>
[zip] =>
[countryID] => 1
[countryName] => United States
[countryCode] => US
)
[hourlyRate] => 0
[rating] => 5
)
[jobOrder] => Array
(
[id] => 57
[title] => Tester
)
)
I tried lot of tricks but couldn't get the desired array format. Can any one help me in this to get the desired array? Thanks in advance.
$arr = array(); //filled with data
$rating = array(); //filled with rates
foreach ( $rating as $key =>$rate ) {
$arr['data'][ $key ]['rating'] = $rate;
}
<?php
$arr=Array(
'data'=>Array(
0=>Array('candidate'=>Array()),
1=>Array('candidate'=>Array())
)
);
$rating=Array(7,5);
foreach($rating as $key=>$val){
$arr['data'][$key]['rating']=$val;
}
print_r($arr);
Returns:
Array
(
[data] => Array
(
[0] => Array
(
[candidate] => Array
(
)
[rating] => 7
)
[1] => Array
(
[candidate] => Array
(
)
[rating] => 5
)
)
)

How to retrieve object from xml

I am trying to get the value of bank_name in php. Can't get it to display.
stdClass Object ( [account_id] => 43726384 [name] => Account Name [state] => action_required [description] => [owner_user_id] =>[type] => personal [create_time] => 1477684534 [disablement_time] => [country] => US [currencies] => Array ( [0] => USD ) [action_reasons] => Array ( [0] => kyc [1] => bank_account ) [disabled_reasons] => Array ( ) [image_uri] => [supported_card_types] => Array ( [0] => visa [1] => mastercard [2] => american_express [3] => discover [4] => jcb [5] => diners_club ) [gaq_domains] => Array ( [0] => ) [balances] => Array ( [0] => stdClass Object ( [balance] => 0 [currency] => USD [disputed_amount] => 0 [incoming_pending_amount] => 0 [outgoing_pending_amount] => 0 [reserved_amount] => 0 [bank_name] => Bank Of America [withdrawal_next_time] => [withdrawal_period] => [withdrawal_type] => ) ) [statuses] => Array ( [0] => stdClass Object ( [currency] => USD [incoming_payments_status] => ok [outgoing_payments_status] => paused [account_review_status] => not_requested ) ) )
It's not very clear if you display data that way, but I think this would be the way to access to bank_name:
$bankName = $object->balances[0]->bank_name;
(If it doesn't work, please show the XML before parsing it to an object)

Multidimensional Array PHP Output Values as a row [closed]

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'];
}

UPS Shipping API Integration Error (PHP)

I download the ShippingGroundFreightWebService PHP examples from UPS.com. I got following error message:
SoapFault Object
(
[message:protected] => An exception has been raised as a result of client data.
[string:private] =>
[code:protected] => 0
[file:protected] => /home5/vizparts/public_html/PHP/SoapGroundFreightShipClient.php
[line:protected] => 193
[trace:private] => Array
(
[0] => Array
(
[file] => /home5/vizparts/public_html/PHP/SoapGroundFreightShipClient.php
[line] => 193
[function] => __soapCall
[class] => SoapClient
[type] => ->
[args] => Array
(
[0] => ProcessShipment
[1] => Array
(
[0] => Array
(
[Request] => Array
(
[RequestOption] => Array
(
[0] => 1
[1] => Shipping
)
)
[Shipment] => Array
(
[ShipFrom] => Array
(
[Name] => Pat Stewart
[TaxIdentification] => 1234567890
[Address] => Array
(
[AddressLine] => 2311 York Road
[City] => Timonium
[StateProvinceCode] => MD
[PostalCode] => 21093
[CountryCode] => US
)
[AttentionName] => String
[Phone] => Array
(
[Number] => 6785851000
[Extension] => 123
)
)
[ShipperNumber] => 222006
[ShipTo] => Array
(
[Name] => Superman
[Address] => Array
(
[AddressLine] => 2010 Warsaw Road
[StateProvinceCode] => GA
[PostalCode] => 30076
[CountryCode] => US
[City] => Roswell
)
[AttentionName] => String
[Phone] => Array
(
[Number] => 6785851000
[Extention] => 111
)
)
[PaymentInformation] => Array
(
[Payer] => Array
(
[Name] => Superman
[Address] => Array
(
[AddressLine] => 2010 Warsaw Road
[City] => Roswell
[StateProvinceCode] => GA
[PostalCode] => 30075
[CountryCode] => US
)
[ShipperNumber] => 00613270
[AttentionName] => String
[Phone] => Array
(
[Number] => 6785851000
)
)
[ShipmentBillingOption] => Array
(
[Code] => 10
[Description] => String
)
)
[Service] => Array
(
[Code] => 308
[Description] => String
)
[HandlingUnitOne] => Array
(
[Quantity] => 16
[Type] => Array
(
[Code] => PLT
[Description] => String
)
)
[Commodity] => Array
(
[CommodityID] => 22
[Description] => BUGS
[Weight] => Array
(
[UnitOfMeasurement] => Array
(
[Code] => LBS
[Description] => String
)
[Value] => 511.25
)
[Dimensions] => Array
(
[UnitOfMeasurement] => Array
(
[Code] => IN
[Description] => String
)
[Length] => 1.25
[Width] => 1.2
[Height] => 5
)
[NumberOfPieces] => 1
[PackagingType] => Array
(
[Code] => PLT
[Description] => String
)
[CommodityValue] => Array
(
[CurrencyCode] => USD
[MonetaryValue] => 265.2
)
[FreightClass] => 60
[NMFCCommodityCode] => 566
)
[Reference] => Array
(
[Number] => Array
(
[Code] => PM
[Value] => 1651651616
)
[BarCodeIndicator] => Array
(
[NumberOfCartons] => 5
[Weight] => Array
(
[UnitOfMeasurement] => Array
(
[Code] => LBS
[Description] => String
)
[Value] => 2
)
)
)
)
)
)
)
)
)
[faultstring] => An exception has been raised as a result of client data.
[faultcode] => Client
[faultcodens] => http://schemas.xmlsoap.org/soap/envelope/
[detail] => stdClass Object
(
[Errors] => stdClass Object
(
[ErrorDetail] => stdClass Object
(
[Severity] => Hard
[PrimaryErrorCode] => stdClass Object
(
[Code] => 9121000
[Description] => Invalid Ship Request Document
)
)
)
)
)
I want to integrate the UPS API into my own website. I want to print the shipping label. How to fix this problem?
you should set the request xsd file,find it in other folder in the package you download.then set the request file with it.

UPS API Error: Invalid Ship Request Document

I'm trying to run the PHP test for the UPS Freight Shipping API that came with the official UPS SDK. I finally got it to connect to the SOAP service with a correct username, password, and API key, but now I'm getting an error that I don't know how to solve. The error coming back from the server is
Invalid Ship Request Document
The full response from the server looks like this:
SoapFault Object
(
[message:protected] => An exception has been raised as a result of client data.
[string:private] =>
[code:protected] => 0
[file:protected] => /my_home_dir/UPS_API/test.php
[line:protected] => 224
[trace:private] => Array
(
[0] => Array
(
[file] => /my_home_dir/UPS_API/test.php
[line] => 224
[function] => __soapCall
[class] => SoapClient
[type] => ->
[args] => Array
(
[0] => ProcessFreightRate
[1] => Array
(
[0] => Array
(
[Request] => Array
(
[RequestOption] => RateChecking Option
)
[ShipFrom] => Array
(
[Name] => Good Incorporation
[Address] => Array
(
[AddressLine] => 2010 WARSAW ROAD
[City] => Roswell
[StateProvinceCode] => GA
[PostalCode] => 30076
[CountryCode] => US
)
)
[ShipTo] => Array
(
[Name] => Sony Company Incorporation
[Address] => Array
(
[AddressLine] => 2311 YORK ROAD
[City] => TIMONIUM
[StateProvinceCode] => MD
[PostalCode] => 21093
[CountryCode] => US
)
)
[PaymentInformation] => Array
(
[Payer] => Array
(
[Name] => Payer inc
[Address] => Array
(
[AddressLine] => 435 SOUTH STREET
[City] => RIS TOWNSHIP
[StateProvinceCode] => NJ
[PostalCode] => 07960
[CountryCode] => US
)
)
[ShipmentBillingOption] => Array
(
[Code] => 10
[Description] => PREPAID
)
)
[Service] => Array
(
[Code] => 308
[Description] => UPS Freight LTL
)
[HandlingUnitOne] => Array
(
[Quantity] => 20
[Type] => Array
(
[Code] => PLT
[Description] => PALLET
)
)
[Commodity] => Array
(
[CommodityID] =>
[Description] => No Description
[Weight] => Array
(
[UnitOfMeasurement] => Array
(
[Code] => LBS
[Description] => Pounds
)
[Value] => 750
)
[Dimensions] => Array
(
[UnitOfMeasurement] => Array
(
[Code] => IN
[Description] => Inches
)
[Length] => 23
[Width] => 17
[Height] => 45
)
[NumberOfPieces] => 45
[PackagingType] => Array
(
[Code] => BAG
[Description] => BAG
)
[DangerousGoodsIndicator] =>
[CommodityValue] => Array
(
[CurrencyCode] => USD
[MonetaryValue] => 5670
)
[FreightClass] => 60
[NMFCCommodityCode] =>
)
[ShipmentServiceOptions] => Array
(
[PickupOptions] => Array
(
[HolidayPickupIndicator] =>
[InsidePickupIndicator] =>
[ResidentialPickupIndicator] =>
[WeekendPickupIndicator] =>
[LiftGateRequiredIndicator] =>
)
[OverSeasLeg] => Array
(
[Dimensions] => Array
(
[Volume] => 20
[UnitOfMeasurement] => Array
(
[Code] => CF
[Description] => String
)
)
[Value] => Array
(
[Cube] => Array
(
[CurrencyCode] => USD
[MonetaryValue] => 5670
)
)
[COD] => Array
(
[CODValue] => Array
(
[CurrencyCode] => USD
[MonetaryValue] => 363
)
[CODPaymentMethod] => Array
(
[Code] => M
[Description] => For Company Check
)
[CODBillingOption] => Array
(
[Code] => 01
[Description] => Prepaid
)
[RemitTo] => Array
(
[Name] => RemitToSomebody
[Address] => Array
(
[AddressLine] => 640 WINTERS AVE
[City] => PARAMUS
[StateProvinceCode] => NJ
[PostalCode] => 07652
[CountryCode] => US
)
[AttentionName] => C J Parker
)
)
[DangerousGoods] => Array
(
[Name] => Very Safe
[Phone] => Array
(
[Number] => 453563321
[Extension] => 1111
)
[TransportationMode] => Array
(
[Code] => CARGO
[Description] => Cargo Mode
)
)
[SortingAndSegregating] => Array
(
[Quantity] => 23452
)
[CustomsValue] => Array
(
[CurrencyCode] => USD
[MonetaryValue] => 23457923
)
[HandlingCharge] => Array
(
[Amount] => Array
(
[CurrencyCode] => USD
[MonetaryValue] => 450
)
)
)
)
)
)
)
)
)
[faultstring] => An exception has been raised as a result of client data.
[faultcode] => Client
[faultcodens] => http://schemas.xmlsoap.org/soap/envelope/
[detail] => stdClass Object
(
[Errors] => stdClass Object
(
[ErrorDetail] => stdClass Object
(
[Severity] => Hard
[PrimaryErrorCode] => stdClass Object
(
[Code] => 9121000
[Description] => Invalid Ship Request Document
)
)
)
)
)
The code that I am using to make the request can be found here (credentials have been removed for security): http://pastebin.com/Yw7sPQdg
Turns out that I was pointing to the wrong end point. The endpoint url for freight rates should be: https://wwwcie.ups.com/webservices/FreightRate

Categories