Loop a multidimensional array - php

I have a multidimensional array and I want access and loop through with a foreach loop. I need to support to access to following items:
-deliveryMethod
-totalPrice
-preparationTime
-[shipping][email]
-[notes] => Email:
Array
(
[page] => 1
[page_size] => 10
[total] => 3
[num_pages] => 1
[result] => Array
(
[0] => Array
(
[_id] => 58c9954070f15c0011535f4d
[friendlyID] => 460
[date] => 1489605952453
[status] => STATUS_DELIVERED
[deliveryMethod] => METHOD_TAKEAWAY
[notes] =>
[grandTotal] => 75.56
[application] => 58be7dc51fa4bc00111b9a64
[__v] => 0
[items] => Array
(
[0] => Array
(
[_id] => 58c9954070f15c0011535f4e
[totalPrice] => 75.56
[preparationTime] => 10
[unitPrice] => 18.89
[quantity] => 4
[name] => Calabrese 29cm
[tax] => Array
(
[value] => 23
[percentage] => 0.23
)
)
)
[shipping] => Array
(
[phone] => 32423432
[email] => info#lovanet.ch
[country] =>
[city] =>
[zip] =>
[fullname] => lori
)
[restaurant] => Array
(
[notes] => Email: info#lovanet.ch
[country] =>
[city] =>
[zip] =>
[address] => Hauptstrasse 43a Bronschhofen
[name] => Restaurant Würe
)
)
[1] => Array
(
[_id] => 58c9948170f15c0011535f4a
[friendlyID] => 459
[date] => 1489605761859
[status] => STATUS_SHIPPED
[deliveryMethod] => METHOD_TAKEAWAY
[notes] =>
[grandTotal] => 54.68
[application] => 58be7dc51fa4bc00111b9a64
[__v] => 0
[items] => Array
(
[0] => Array
(
[_id] => 58c9948170f15c0011535f4c
[totalPrice] => 12.9
[preparationTime] => 10
[unitPrice] => 12.9
[quantity] => 1
[name] => Calabrese 22cm
[tax] => Array
(
[value] => 23
[percentage] => 0.23
)
)
[1] => Array
(
[_id] => 58c9948170f15c0011535f4b
[totalPrice] => 41.78
[preparationTime] => 10
[description] => Extra Salami- $2.00
[unitPrice] => 18.89
[quantity] => 2
[name] => Calabrese 29cm
[tax] => Array
(
[value] => 23
[percentage] => 0.23
)
)
)
[shipping] => Array
(
[phone] => 32423432
[email] => info#lovanet.ch
[country] =>
[city] =>
[zip] =>
[fullname] => lori
)
[restaurant] => Array
(
[notes] => Email: info#lovanet.ch
[country] =>
[city] =>
[zip] =>
[address] => Hauptstrasse 43a Bronschhofen
[name] => Restaurant Würe
)
)
[2] => Array
(
[_id] => 58be912e1fa4bc00111b9ae6
[friendlyID] => 443
[date] => 1488884014509
[status] => STATUS_IN_PROGRESS
[deliveryMethod] => METHOD_TAKEAWAY
[notes] =>
[grandTotal] => 12.9
[application] => 58be7dc51fa4bc00111b9a64
[__v] => 0
[items] => Array
(
[0] => Array
(
[name] => Calabrese 22cm
[quantity] => 1
[unitPrice] => 12.9
[preparationTime] => 10
[totalPrice] => 12.9
[_id] => 58be912e1fa4bc00111b9ae7
[tax] => Array
(
[percentage] => 0.23
[value] => 23
)
)
)
[shipping] => Array
(
[fullname] => lori
[zip] =>
[city] =>
[country] =>
[email] => info#lovanet.ch
[phone] => 32423432
)
[restaurant] => Array
(
[name] => Restaurant Würe
[address] => Hauptstrasse 43a Bronschhofen
[zip] =>
[city] =>
[country] =>
[notes] => Email: info#lovanet.ch
)
)
)
)
Could you please help me to create this foreach loop?

foreach ($myArrayName['result'] as $entry) {
foreach ($myArrayName['items'] as $item) {
echo 'totalPrice: ' . $item['totalPrice'] . '<br />';
echo 'preparationTime: ' . $item['preparationTime'] . '<br />';
}
echo 'deliveryMethod:' . $entry['deliveryMethod'] . '<br />';
echo 'E-Mail: ' . $entry['shipping']['email'] . '<br />';
echo 'E-Mail 2: ' . substr($entry['restaurant']['notes'], 7) . '<br />';
}
try this code and replace $myArrayName with the name of your array

Related

Set session variable without reloading the page

I send a value to a php file with AJAX.
I am having this function in jQuery:
// Service select change
$container.on('change', '.bookly-js-select-service', function() {
var gekozen_behandeling = this.value;
$.ajax({
type: 'POST',
url: '/behandeling.php',
data: {
behandeling_nieuw: gekozen_behandeling
},
dataType: 'html',
success: function(response) {
console.log(response);
}
});
});
After doing this it alerts done.
In my file behandeling.php the code is like this:
<?php
session_start();
$_SESSION["type_behandeling"] = $_POST["behandeling_nieuw"];
echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';
?>
After doing this and navigate to the behandeling.php file the session variable is still empty.
<pre>Array
(
[bookly] => Array
(
[forms] => Array
(
[5e1c6d8916b27] => Array
(
[defaults] => Array
(
[service_id] => 0
[staff_id] => 0
[location_id] => 0
[category_id] => 5
)
[last_touched] => 1578921357
[data] => Array
(
[first_rendered_step] => 1
[time_zone] =>
[time_zone_offset] =>
[date_from] => 2020-01-13
[days] =>
[time_from] => 09:00
[time_to] => 21:00
[slots] => Array
(
)
[facebook_id] =>
[full_name] =>
[first_name] =>
[last_name] =>
[email] =>
[email_confirm] =>
[phone] =>
[birthday] =>
[additional_address] =>
[country] =>
[state] =>
[postcode] =>
[city] =>
[street] =>
[street_number] =>
[address_iso] => Array
(
)
[notes] =>
[info_fields] => Array
(
)
[coupon_code] =>
[deposit_full] => 0
[edit_cart_keys] => Array
(
)
[repeated] => 0
[repeat_data] => Array
(
)
)
[cart] => Array
(
)
[chain] => Array
(
[0] => Array
(
[service_id] =>
[staff_ids] => Array
(
)
[number_of_persons] =>
[quantity] =>
[extras] => Array
(
)
[custom_fields] => Array
(
)
[location_id] =>
[series_unique_id] => 0
[first_in_series] =>
[units] => 1
)
)
[booking_numbers] => Array
(
)
[payment_id] =>
[payment_type] => local
)
[5e1c6dc0324c7] => Array
(
[defaults] => Array
(
[service_id] => 0
[staff_id] => 0
[location_id] => 0
[category_id] => 5
)
[last_touched] => 1578921411
[data] => Array
(
[first_rendered_step] => 1
[time_zone] =>
[time_zone_offset] =>
[date_from] => 2020-01-13
[days] =>
[time_from] => 09:00
[time_to] => 21:00
[slots] => Array
(
)
[facebook_id] =>
[full_name] =>
[first_name] =>
[last_name] =>
[email] =>
[email_confirm] =>
[phone] =>
[birthday] =>
[additional_address] =>
[country] =>
[state] =>
[postcode] =>
[city] =>
[street] =>
[street_number] =>
[address_iso] => Array
(
)
[notes] =>
[info_fields] => Array
(
)
[coupon_code] =>
[deposit_full] => 0
[edit_cart_keys] => Array
(
)
[repeated] => 0
[repeat_data] => Array
(
)
)
[cart] => Array
(
)
[chain] => Array
(
[0] => Array
(
[service_id] =>
[staff_ids] => Array
(
)
[number_of_persons] =>
[quantity] =>
[extras] => Array
(
)
[custom_fields] => Array
(
)
[location_id] =>
[series_unique_id] => 0
[first_in_series] =>
[units] => 1
)
)
[booking_numbers] => Array
(
)
[payment_id] =>
[payment_type] => local
)
[5e1c6df9da30f] => Array
(
[defaults] => Array
(
[service_id] => 0
[staff_id] => 0
[location_id] => 0
[category_id] => 5
)
[last_touched] => 1578921468
[data] => Array
(
[first_rendered_step] => 1
[time_zone] =>
[time_zone_offset] =>
[date_from] => 2020-01-13
[days] =>
[time_from] => 09:00
[time_to] => 21:00
[slots] => Array
(
)
[facebook_id] =>
[full_name] =>
[first_name] =>
[last_name] =>
[email] =>
[email_confirm] =>
[phone] =>
[birthday] =>
[additional_address] =>
[country] =>
[state] =>
[postcode] =>
[city] =>
[street] =>
[street_number] =>
[address_iso] => Array
(
)
[notes] =>
[info_fields] => Array
(
)
[coupon_code] =>
[deposit_full] => 0
[edit_cart_keys] => Array
(
)
[repeated] => 0
[repeat_data] => Array
(
)
)
[cart] => Array
(
)
[chain] => Array
(
[0] => Array
(
[service_id] =>
[staff_ids] => Array
(
)
[number_of_persons] =>
[quantity] =>
[extras] => Array
(
)
[custom_fields] => Array
(
)
[location_id] =>
[series_unique_id] => 0
[first_in_series] =>
[units] => 1
)
)
[booking_numbers] => Array
(
)
[payment_id] =>
[payment_type] => local
)
[5e1c6e0f76ddd] => Array
(
[defaults] => Array
(
[service_id] => 0
[staff_id] => 0
[location_id] => 0
[category_id] => 5
)
[last_touched] => 1578921490
[data] => Array
(
[first_rendered_step] => 1
[time_zone] =>
[time_zone_offset] =>
[date_from] => 2020-01-13
[days] =>
[time_from] => 09:00
[time_to] => 21:00
[slots] => Array
(
)
[facebook_id] =>
[full_name] =>
[first_name] =>
[last_name] =>
[email] =>
[email_confirm] =>
[phone] =>
[birthday] =>
[additional_address] =>
[country] =>
[state] =>
[postcode] =>
[city] =>
[street] =>
[street_number] =>
[address_iso] => Array
(
)
[notes] =>
[info_fields] => Array
(
)
[coupon_code] =>
[deposit_full] => 0
[edit_cart_keys] => Array
(
)
[repeated] => 0
[repeat_data] => Array
(
)
)
[cart] => Array
(
)
[chain] => Array
(
[0] => Array
(
[service_id] =>
[staff_ids] => Array
(
)
[number_of_persons] =>
[quantity] =>
[extras] => Array
(
)
[custom_fields] => Array
(
)
[location_id] =>
[series_unique_id] => 0
[first_in_series] =>
[units] => 1
)
)
[booking_numbers] => Array
(
)
[payment_id] =>
[payment_type] => local
)
)
)
[type_behandeling] => 51
)
</pre>
It might be possible that when you navigate to behandeling.php
that you are assigning the posted variable to session as
$_SESSION["type_behandeling"] = $_POST["behandeling_nieuw"];
the posted data not found as you are visiting (http get method). Hence session is overwritten with null or empty.
Change dataType: 'html' to dataType: 'JSON'
Through Chrome/Firefox Dev tools check - behandeling.php file is getting called
Through Chrome/Firefox Dev tools check - form data is getting submitted to PHP File
Also if you access the file outside, it Recheck the $_POST Value , since the value is not set, a blank will be assigned to the session.
Check the $_POST Value
if (isset($_POST["behandeling_nieuw"])) {
$_SESSION["type_behandeling"] = $_POST["behandeling_nieuw"];
echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';
} else {
echo "POST Value not set";
if(isset($_SESSION['type_behandeling']) && !empty($_SESSION['type_behandeling'])) {
echo 'Set and not empty, and no undefined index error!';
}
else {
echo "Session is not set ";
}
}

How to print complicated multidimensional array through foreach loop in php

I have an Multi Dimensional array like below.
<?php$array = Array ( [ResultIndex] => OB4 [Source] => 3 [IsLCC] => 1 [IsRefundable] => [GSTAllowed] => [AirlineRemark] => this is a test from aditya [Fare] => Array ( [Currency] => INR [BaseFare] => 865 [Tax] => 613 [TaxBreakup] => Array ( [0] => Array ( [key] => YQTax [value] => 0 ) [1] => Array ( [key] => YR [value] => 0 ) [2] => Array ( [key] => PSF [value] => 0 ) [3] => Array ( [key] => UDF [value] => 0 ) [4] => Array ( [key] => JNTax [value] => 52 ) [5] => Array ( [key] => INTax [value] => 0 ) [6] => Array ( [key] => TransactionFee [value] => 0 ) [7] => Array ( [key] => OtherTaxes [value] => 0 ) ) [YQTax] => 0 [AdditionalTxnFeeOfrd] => 0 [AdditionalTxnFeePub] => 0 [PGCharge] => 0 [OtherCharges] => 0 [ChargeBU] => Array ( [0] => Array ( [key] => TBOMARKUP [value] => 0 ) [1] => Array ( [key] => CONVENIENCECHARGE [value] => 0 ) [2] => Array ( [key] => OTHERCHARGE [value] => 0 ) ) [Discount] => 0 [PublishedFare] => 1478 [CommissionEarned] => 22.06 [PLBEarned] => 14.09 [IncentiveEarned] => 14.33 [OfferedFare] => 1427.52 [TdsOnCommission] => 4.41 [TdsOnPLB] => 2.82 [TdsOnIncentive] => 2.87 [ServiceFee] => 0 [TotalBaggageCharges] => 0 [TotalMealCharges] => 0 [TotalSeatCharges] => 0 [TotalSpecialServiceCharges] => 0 ) [FareBreakdown] => Array ( [0] => Array ( [Currency] => INR [PassengerType] => 1 [PassengerCount] => 1 [BaseFare] => 865 [Tax] => 613 [YQTax] => 0 [AdditionalTxnFeeOfrd] => 0 [AdditionalTxnFeePub] => 0 [PGCharge] => 0 ) ) [Segments] => Array ( [0] => Array ( [0] => Array ( [Baggage] => 10 Kg [CabinBaggage] => 7 Kg [TripIndicator] => 1 [SegmentIndicator] => 1 [Airline] => Array ( [AirlineCode] => SG [AirlineName] => SpiceJet [FlightNumber] => 1033 [FareClass] => C [OperatingCarrier] => ) [NoOfSeatAvailable] => 4 [Origin] => Array ( [Airport] => Array ( [AirportCode] => BLR [AirportName] => Hindustan [Terminal] => [CityCode] => BLR [CityName] => Bangalore [CountryCode] => IN [CountryName] => India ) [DepTime] => 2017-10-21T17:40:00 ) [Destination] => Array ( [Airport] => Array ( [AirportCode] => MAA [AirportName] => Chennai [Terminal] => [CityCode] => MAA [CityName] => Chennai [CountryCode] => IN [CountryName] => India ) [ArrTime] => 2017-10-21T18:40:00 ) [Duration] => 60 [GroundTime] => 0 [Mile] => 0 [StopOver] => [StopPoint] => [StopPointArrivalTime] => 2017-10-21T18:40:00 [StopPointDepartureTime] => 2017-10-21T17:40:00 [Craft] => DH8 [Remark] => [IsETicketEligible] => 1 [FlightStatus] => Confirmed [Status] => ) ) ) [LastTicketDate] => [TicketAdvisory] => [FareRules] => Array ( [0] => Array ( [Origin] => BLR [Destination] => MAA [Airline] => SG [FareBasisCode] => CSAVER [FareRuleDetail] => [FareRestriction] => ) ) [AirlineCode] => SG [ValidatingAirline] => SG );?>
how to make it print using foreach loop in php. if any one have idea to rectify this please suggest me

PHP - Looping through indexed JSON array

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

PHP5.3 json_encode is outputting bad json

I am attempting to json_encode an array in php5.3.
json_encode($paperwork_info[0])
The result is bad json. The array is as follows:
[paperwork_guid] => c5bfe512-908d-c513-5a5e-e3a2fbb5548b
[name] => recycle
[sections] => Array
(
[0] => header
[1] => customer
[2] => scope_bullets
[3] => product
[4] => signature
[5] => installer
[6] => order_data
)
[data] => Array
(
[header] => Array
(
[image] => Array
(
[src] => recycle.png
[format] => float:left
)
)
[customer] => Array
(
[f_name] => TEST ONLY
[l_name] => NEIMEIER
[middle_name] => none
[title] => none
[address1] => 28 OEHMAN BLVD
[address2] =>
[address3] =>
[zip] => 14225
[city] => BUFFALO
[state_abbrev] => NY
[email_address] => DALE.NEIMEIER#INSTALLS.COM
[phones] => Array
(
[alt] =>
[cell] =>
[work] =>
[home] => 7165551212
)
)
[scope_bullets] => Array
(
[sku_3001] => Array
(
[bullet_0] => Array
(
[paperwork_bullet_obj_guid] => 2ebefa96-6f6b-069e-e194-245d138b9845
[ordering] => 1
[bullet_text] => This is bullet point #1
[child_level] => 1
[formatting] => Array
(
[0] => Array
(
[start_word] => 2
[num_words] => 0
[tag] => b
[href] =>
)
[1] => Array
(
[start_word] => 2
[num_words] => 1
[tag] => a
[href] => http://www.cnn.com
)
)
)
[bullet_1] => Array
(
[paperwork_bullet_obj_guid] => 734db3f4-01a0-b025-9624-cc52a1845dff
[ordering] => 1
[bullet_text] => Sub-point #1.1
[child_level] => 2
)
[bullet_2] => Array
(
[paperwork_bullet_obj_guid] => ebf5ef02-906e-2005-e499-27eae2edefe9
[ordering] => 1
[bullet_text] => Sub point #1.1.1
[child_level] => 3
)
[bullet_3] => Array
(
[paperwork_bullet_obj_guid] => 447997c1-fd9c-25be-b9bf-39257009fb8b
[ordering] => 1
[bullet_text] => This is bullet point #2
[child_level] => 1
)
[bullet_4] => Array
(
[paperwork_bullet_obj_guid] => 5def2d9c-d322-788c-0afe-d13707004b97
[ordering] => 1
[bullet_text] => Sub point #2.1
[child_level] => 2
)
[bullet_5] => Array
(
[paperwork_bullet_obj_guid] => 84936151-65a3-bcca-951f-b69ff16d34ec
[ordering] => 2
[bullet_text] => Sub point #2.2
[child_level] => 2
)
)
[sku_4405] =>
)
[product] => Array
(
[0] => Array
(
[model] => Paperwork Test abc123
[serial] => 777333
[weight] => 26
[size] =>
[width] =>
[height] => 32
[num_boxes] => 1
[product_name] =>
[sku] =>
[model_number] =>
[part_number] =>
[serial_number] =>
[cat_value] => Best Buy Equipment
[product] =>
[product_type] =>
[product_size] =>
[category] =>
)
)
[signature] => Array
(
[date] => 2016-06-15
[need_customer] => 1
[need_tech] => 1
[need_store] =>
[need_warehouse] =>
)
[installer] => Array
(
[anum] => 45455
[inst_obj_guid] => fb91cf85-381c-b740-e063-775151743da2
[phone] => (317) 519-0481
[tech_f_name] =>
[tech_l_name] =>
[company_name] => ZICO LLC
[email_address] => ZICOLLC#GMAIL.COM
)
[order_data] => Array
(
[assigned_date] => 2016-06-07 22:24:47-04
[purchased_date] => 2016-06-06
[creation_date] => 2016-06-06
[install_date] => 2016-06-10
[install_time] => 19:00:00
[prescheduled_date] => 1969-12-31
[prom_start_time] => 2016-06-10 08:06:00
[prom_end_time] => 2016-06-10 11:06:00
[client_order_id] => 12365478996325412
[job_num] => 4043269
[campaign_wkord_obj_guid] => a9a8cc0b-d7ef-ac0e-df61-dad41c023cb0
[pos_obj_guid] => 096e38c2-55c3-80cf-0778-23f81f1cf2f6
[inst_obj_guid] => fb91cf85-381c-b740-e063-775151743da2
[client_obj_guid] => 247e893a-3ea4-c544-47b2-f23ff16017c6
[ord_obj_guid] => ae55e034-d7d0-5d13-3dd3-22f99df8ead4
[skus] => Array
(
[0] => Array
(
[job_type_obj_guid] => 8234ca2c-e40e-c48c-befc-7ceac5e9de32
[job_sku] => 3001
[client_sku] =>
[sku_name] => Site Survey
)
[1] => Array
(
[job_type_obj_guid] => a3f60c3b-ad3c-d828-9898-fa200edcd3cd
[job_sku] => 4405
[client_sku] =>
[sku_name] => Home Delivery
)
)
[client_specific_data] =>
)
)
)
The resultant json string is as follows:
{"paperwork_guid":"c5bfe512-908d-c513-5a5e-e3a2fbb5548b","name":"recycle","sections":{"0":"header","1"
:"customer","2":"scope_bullets","3":"product","4":"signature","5":"installer","6":"order_data"},"data"
:{"header":{"image":{"src":"recycle.png","format":"float:left"}},"customer":{"f_name":"TEST ONLY","l_name"
:"NEIMEIER","middle_name":"none","title":"none","address1":"28 OEHMAN BLVD","address2":"","address3"
:null,"zip":"14225","city":"BUFFALO","state_abbrev":"NY","email_address":"DALE.NEIMEIER#INSTALLS.COM"
,"phones":{"alt":"","cell":"","work":"","home":"7165551212"}},"scope_bullets":{"sku_3001":{"bullet_0"
:{"paperwork_bullet_obj_guid":"2ebefa96-6f6b-069e-e194-245d138b9845","ordering":"1","bullet_text":"This
is bullet point #1","child_level":"1","formatting":{"0":{"start_word":"2","num_words":"0","tag":"b"
,"href":""},"1":{"start_word":"2","num_words":"1","tag":"a","href":"http:\/\/www.cnn.com"}}},"bullet_1"
:{"paperwork_bullet_obj_guid":"734db3f4-01a0-b025-9624-cc52a1845dff","ordering":"1","bullet_text":"Sub-point
#1.1","child_level":"2"},"bullet_2":{"paperwork_bullet_obj_guid":"ebf5ef02-906e-2005-e499-27eae2edefe9"
,"ordering":"1","bullet_text":"Sub point #1.1.1","child_level":"3"},"bullet_3":{"paperwork_bullet_obj_guid"
:"447997c1-fd9c-25be-b9bf-39257009fb8b","ordering":"1","bullet_text":"This is bullet point #2","child_level"
:"1"},"bullet_4":{"paperwork_bullet_obj_guid":"5def2d9c-d322-788c-0afe-d13707004b97","ordering":"1","bullet_text"
:"Sub point #2.1","child_level":"2"},"bullet_5":{"paperwork_bullet_obj_guid":"84936151-65a3-bcca-951f-b69ff16d34ec"
,"ordering":"2","bullet_text":"Sub point #2.2","child_level":"2"}},"sku_4405":null},"product":{"0":{"model"
:"Paperwork Test abc123","serial":"777333","weight":"26","size":null,"width":null,"height":"32","num_boxes"
:"1","product_name":null,"sku":null,"model_number":null,"part_number":null,"serial_number":null,"cat_value"
:"Best Buy Equipment","product":null,"product_type":null,"product_size":null,"category":null}},"signature"
:{"date":"2016-06-15","need_customer":true,"need_tech":true,"need_store":false,"need_warehouse":false
},"installer":{"anum":"45455","inst_obj_guid":"fb91cf85-381c-b740-e063-775151743da2","phone":"(317) 519-0481"
,"tech_f_name":null,"tech_l_name":null,"company_name":"ZICO LLC","email_address":"ZICOLLC#GMAIL.COM"
},"order_data":{"assigned_date":"2016-06-07 22:24:47-04","purchased_date":"2016-06-06","creation_date"
:"2016-06-06","install_date":"2016-06-10","install_time":"19:00:00","prescheduled_date":"1969-12-31"
,"prom_start_time":"2016-06-10 08:06:00","prom_end_time":"2016-06-10 11:06:00","client_order_id":"12365478996325412"
,"job_num":"4043269","campaign_wkord_obj_guid":"a9a8cc0b-d7ef-ac0e-df61-dad41c023cb0","pos_obj_guid"
:"096e38c2-55c3-80cf-0778-23f81f1cf2f6","inst_obj_guid":"fb91cf85-381c-b740-e063-775151743da2","client_obj_guid"
:"247e893a-3ea4-c544-47b2-f23ff16017c6","ord_obj_guid":"ae55e034-d7d0-5d13-3dd3-22f99df8ead4","skus"
:{"0":{"job_type_obj_guid":"8234ca2c-e40e-c48c-befc-7ceac5e9de32","job_sku":"3001","client_sku":null
,"sku_name":"Site Survey"},"1":{"job_type_obj_guid":"a3f60c3b-ad3c-d828-9898-fa200edcd3cd","job_sku"
:"4405","client_sku":null,"sku_name":"Home Delivery"}},"client_specific_data":""}}}
It appears as though new line feeds are being inserted randomly throughout the josn string. I am not sure if it is just how firebug is outputting the string, but copying-and-pasting the string into jsonLINT produces errors.
Any help would be appreciated.

PHP Array issue , mulitple rows generated

I have an array that is fetched from database using some bindModel in cakephp.
finally now I want to change its format according to my liking to own.
here is the array contains.
Array
(
[0] => Array
(
[DriverLocation] => Array
(
[id] => 1
[dispensary_id] => 1
[driver_id] => 85
[zip_code_id] => 42
[created] => 2015-05-25 12:01:14
)
[ZipCode] => Array
(
[id] => 42
[province_id] => 3846
[city] => Rohtak
[zip_code] => 30215
[status] => active
)
[UserProfile] => Array
(
[first_name] => Arman
[last_name] => Kumar
)
)
[1] => Array
(
[DriverLocation] => Array
(
[id] => 2
[dispensary_id] => 1
[driver_id] => 85
[zip_code_id] => 43
[created] => 2015-05-25 12:01:14
)
[ZipCode] => Array
(
[id] => 43
[province_id] => 3846
[city] => Rohtak
[zip_code] => 15478
[status] => active
)
[UserProfile] => Array
(
[first_name] => Arman
[last_name] => Kumar
)
)
[2] => Array
(
[DriverLocation] => Array
(
[id] => 3
[dispensary_id] => 1
[driver_id] => 77
[zip_code_id] => 41
[created] => 2015-05-25 12:45:47
)
[ZipCode] => Array
(
[id] => 41
[province_id] => 3846
[city] => Malviya Vihar
[zip_code] => 12558
[status] => active
)
[UserProfile] => Array
(
[first_name] => Manish
[last_name] => Kumar
)
)
)
I want to convert or change its format something like as follows.
This will altered on the behalf of DriverLocation=>driver_id
Array
(
[0] => Array
(
[DriverLocation] => Array
(
[id] => 1
[dispensary_id] => 1
[driver_id] => 85
[zip_code_id] => 42
[created] => 2015-05-25 12:01:14
)
[ZipCode] => Array
(
[0] => Array
(
[id] => 42
[province_id] => 3846
[city] => Rohtak
[zip_code] => 30215
[status] => active
)
[1] => Array
(
[id] => 43
[province_id] => 3846
[city] => Rohtak
[zip_code] => 15478
[status] => active
)
)
[UserProfile] => Array
(
[first_name] => Arman
[last_name] => Kumar
)
)
[1] => Array
(
[DriverLocation] => Array
(
[id] => 3
[dispensary_id] => 1
[driver_id] => 77
[zip_code_id] => 41
[created] => 2015-05-25 12:45:47
)
[ZipCode] => Array
(
[id] => 41
[province_id] => 3846
[city] => Malviya Vihar
[zip_code] => 12558
[status] => active
)
[UserProfile] => Array
(
[first_name] => Manish
[last_name] => Kumar
)
)
)
You can use this loop:
foreach($ar as $k1=>$a1){
foreach($ar as $k2=>$a2){
if($k1 < $k2 && $a1["DriverLocation"]["driver_id"] == $a2["DriverLocation"]["driver_id"]){
$ar[$k1]["ZipCode"][] = $a2["ZipCode"];
if(isset($ar[$k1]["ZipCode"]["id"])){
$ar[$k1]["ZipCode"][] = array("id"=>$ar[$k1]["ZipCode"]["id"]);
unset($ar[$k1]["ZipCode"]["id"]);
}
unset($ar[$k2]);
}
}
}
Use array_push() function to make it
for sample example
<?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>
output
Array ( [0] => red [1] => green [2] => blue [3] => yellow )

Categories