How to combine multidimensional arrays? - php

I have the following arrays below and I am really struggling with how to add the Pricing data to the corresponding Product data so I would have one array for each numeric index.
[Product] => Array
(
[0] => Array
(
[Title] => Rent: Filmed Live on Broadway
[UPC] => 043396297913
[ASIN] => B001LMAKAG
[SalesRank] => 12429
[ImageURL] => http://ecx.images-amazon.com/images/I/51rzFLo7EML._SL200_.jpg
[ProductGroup] => DVD
[Publisher] => Sony Pictures Home Entertainment
[NumberOfDiscs] => 1
)
[1] => Array
(
[Title] => Scrooged
[UPC] => 097363205425
[ASIN] => 6305609772
[SalesRank] => 308636
[ImageURL] => http://ecx.images-amazon.com/images/I/51BqfhI7NaL._SL200_.jpg
[ProductGroup] => DVD
[Publisher] => Paramount
[NumberOfDiscs] => 1
)
[2] => Array
(
[Title] => Upstairs Downstairs: Season 2
[UPC] => 883929253753
[ASIN] => B0090XUARQ
[SalesRank] => 23167
[ImageURL] => http://ecx.images-amazon.com/images/I/51O-STwE1SL._SL200_.jpg
[ProductGroup] => DVD
[Publisher] => BBC Home Entertainment
[NumberOfDiscs] => 2
)
[3] => Array
(
[Title] => Junction Boys
[UPC] => 796019795210
[ASIN] => B000FVR1T2
[SalesRank] => 32220
[ImageURL] => http://ecx.images-amazon.com/images/I/41151H2xalL._SL200_.jpg
[ProductGroup] => DVD
[Publisher] => Genius
[NumberOfDiscs] => 1
)
[4] => Array
(
[Title] => 20-Film Horror: The Prophecy II/ Dracula III: Legacy/ The House That Would Not Die/ Seedpeople/ The Greenskeeper/ Grim/ Evil Bong 3 & More
[UPC] => 096009092443
[ASIN] => B008R52L7K
[SalesRank] => 26999
[ImageURL] => http://ecx.images-amazon.com/images/I/617n-5nMLYL._SL200_.jpg
[ProductGroup] => DVD
[Publisher] => Echo Bridge Home Entertainment
[NumberOfDiscs] => 4
)
)
[Pricing] => Array
(
[0] => Array
(
[LowestPrice] => 3.03
[ShippingPrice] => 3.99
[TotalPrice] => 7.02
)
[1] => Array
(
[LowestPrice] => 0.01
[ShippingPrice] => 3.99
[TotalPrice] => 4
)
[2] => Array
(
[LowestPrice] => 22.19
[ShippingPrice] => 3.99
[TotalPrice] => 26.18
)
[3] => Array
(
[LowestPrice] => 1.33
[ShippingPrice] => 3.99
[TotalPrice] => 5.32
)
[4] => Array
(
[LowestPrice] => 0.74
[ShippingPrice] => 3.99
[TotalPrice] => 4.73
)
)
)
Any help would be greatly appreciated.
Thanks,
Eric

for($index=0; $index<=4; $index++){
$final_array[$index] = array_merge($product[$index],$pricing[$index]);
}
This would give you the desired array.

Try
foreach ($products as $key => $product){
$products[$key] = array_merge($product, $prices[$key])
}

Related

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

Looping in a complex array

hope you are fine today.
I have an xml file online that I stored it into an array using file_get_contents(), simplexml_load_string() json_encode and json_decode; using the above methods gave an array like this:
[data] => Array
(
[transaction] => Array
(
[0] => Array
(
[fields] => Array
(
[transactionid] => 90397725
[transactionreference] => 90397725
[transactiontime] => 14:34:56
[transactiondate] => 2016-04-08
[upgauthcode] => 649192
[cardnumber] => ***************5104
[cardholdersname] => MISS Jane K Doe
[switchnumber] => Array
(
)
[cardstartyear] => 15
[cardstartmonth] => 02
[cardexpireyear] => 20
[cardexpiremonth] => 02
[transactionamount] => 108.00
[cardtype] => VISADEBIT
[baskettype] => order
[regisalu] => Miss
[regifnam] => Jane
[regilnam] => Doe
[regiadd1] => 14 Test Street
[regiadd4] => Test
[regiadd5] => West Test
[regiadd6] => fg28 5nZ
[regiadd7] => United Kingdom
[regidnum] => 07464000000
[regienum] => 07464000000
[regimobi] => 07464000000
[maddsalu] => Miss
[maddfnam] => Jane
[maddlnam] => Doe
[maddadd1] => 14 Test Street
[maddadd4] => Test
[maddadd5] => West Test
[maddadd6] => fg28 5nZ
[maddadd7] => United Kingdom
[madddnum] => 07464000000
[maddenum] => 07464000000
[maddidel] => Deliver
[baskettotal] => 90.00
[hirewaivertotal] => 0.00
[deliverytotal] => 0.00
[collectiontotal] => 0.00
[cardholderaddr1] => 14 Test Street
[cardholderaddr2] => Array
(
)
[cardholdercity] => Test
[cardholderstate] => West Test
[cardholderpostcode] => fg28 5nZ
[cardholdercountry] => United Kingdom
[regiuser] => jane.doe#gmail.com
[regihtml] => 0
[regigrde] => 0
[regigrdn] => 0
[regipopt] => 0
[maddcoll] => 1
[maddgrde] => 0
[maddgrdn] => 0
)
[orderitems] => Array
(
[item] => Array
(
[code] => DL008
[sku] => Array
(
)
[desc] => 22M LED Festoon (230v)
[description] => DL008, 22M LED Festoon (230v)
[price] => 45.00
[qty] => 2
[totalprice] => 90.00
)
)
)
[45] => Array
(
[fields] => Array
(
[transactionid] => 93645131
[transactionreference] => 93645131
[transactiontime] => 14:15:16
[transactiondate] => 2016-07-07
[upgauthcode] => 085526
[cardnumber] => ***************0103
[cardholdersname] => John Doe
[switchnumber] => Array
(
)
[cardstartyear] => 15
[cardstartmonth] => 12
[cardexpireyear] => 18
[cardexpiremonth] => 11
[transactionamount] => 588.00
[cardtype] => MASTERCARD
[baskettype] => order
[regisalu] => Mr
[regifnam] => John
[regilnam] => Doe
[regiadd1] => 39-45 TEST SQUARE
[regiadd2] => CITY Test Aquarium
[regiadd4] => London
[regiadd6] => bb2A 1op
[regiadd7] => United Kingdom
[regidnum] => 074640000000
[regienum] => 074640000000
[regimobi] => 07464000000
[maddsalu] => Mr
[maddfnam] => John
[maddlnam] => Doe
[maddadd1] => 39-45 TEST SQUARE
[maddadd2] => CITY Test AQUARIUM
[maddadd4] => London
[maddadd6] => bb2A 1op
[maddadd7] => United Kingdom
[madddnum] => 07464000000
[maddenum] => 07464000000
[maddidel] => Deliver
[baskettotal] => 490.00
[hirewaivertotal] => 0.00
[deliverytotal] => 0.00
[collectiontotal] => 0.00
[cardholderaddr1] => 39-45 TEST SQUARE
[cardholderaddr2] => CITY TEST AQUARIUM
[cardholdercity] => London
[cardholderpostcode] => bb2A 1op
[cardholdercountry] => United Kingdom
[regiuser] => john.doe#bloomberg.net
[regihtml] => 0
[regipopt] => 1
[regigrde] => 0
[regigrdn] => 0
[maddcoll] => 1
[maddgrde] => 0
[maddgrdn] => 0
)
[orderitems] => Array
(
[item] => Array
(
[0] => Array
(
[code] => PK289
[sku] => Array
(
)
[desc] => EvoLite Helmet STD Peak (white)
[description] => PK289, EvoLite Helmet STD Peak (white)
[price] => 9.00
[qty] => 6
[totalprice] => 54.00
)
[1] => Array
(
[code] => GGE3
[sku] => Array
(
)
[desc] => Graft Gear Safety Spectacle
[description] => GGE3, Graft Gear Safety Spectacle
[price] => 2.00
[qty] => 6
[totalprice] => 12.00
)
[2] => Array
(
[code] => GG025
[sku] => Array
(
)
[desc] => Graft Gear Hi-Vis Vest (M)
[description] => GG025, Graft Gear Hi-Vis Vest (M)
[price] => 5.00
[qty] => 2
[totalprice] => 10.00
)
[3] => Array
(
[code] => GG027
[sku] => Array
(
)
[desc] => Graft Gear Hi-Vis Vest (XL)
[description] => GG027, Graft Gear Hi-Vis Vest (XL)
[price] => 5.00
[qty] => 4
[totalprice] => 20.00
)
[4] => Array
(
[code] => GG029
[sku] => Array
(
)
[desc] => Graft Gear Hi-Vis Vest (3XL)
[description] => GG029, Graft Gear Hi-Vis Vest (3XL)
[price] => 5.00
[qty] => 4
[totalprice] => 20.00
)
[5] => Array
(
[code] => GG030
[sku] => Array
(
)
[desc] => Graft Gear Hi-Vis Vest (4XL)
[description] => GG030, Graft Gear Hi-Vis Vest (4XL)
[price] => 5.00
[qty] => 2
[totalprice] => 10.00
)
[6] => Array
(
[code] => GG037
[sku] => Array
(
)
[desc] => Graft Gear Bomber (M)
[description] => GG037, Graft Gear Bomber (M)
[price] => 23.00
[qty] => 1
[totalprice] => 23.00
)
[7] => Array
(
[code] => GG039
[sku] => Array
(
)
[desc] => Graft Gear Bomber (XL)
[description] => GG039, Graft Gear Bomber (XL)
[price] => 23.00
[qty] => 2
[totalprice] => 46.00
)
[8] => Array
(
[code] => GG041
[sku] => Array
(
)
[desc] => Graft Gear Bomber (XXXL)
[description] => GG041, Graft Gear Bomber (XXXL)
[price] => 23.00
[qty] => 3
[totalprice] => 69.00
)
[9] => Array
(
[code] => GG073
[sku] => Array
(
)
[desc] => Nubuck Mid-Cut Safety Boot
[description] => GG073, Nubuck Mid-Cut Safety Boot
[price] => 39.00
[qty] => 1
[totalprice] => 39.00
)
[10] => Array
(
[code] => GG071
[sku] => Array
(
)
[desc] => Nubuck Mid-Cut Safety Boot
[description] => GG071, Nubuck Mid-Cut Safety Boot
[price] => 39.00
[qty] => 2
[totalprice] => 78.00
)
[11] => Array
(
[code] => GG070
[sku] => Array
(
)
[desc] => Nubuck Mid-Cut Safety Boot
[description] => GG070, Nubuck Mid-Cut Safety Boot
[price] => 39.00
[qty] => 2
[totalprice] => 78.00
)
[12] => Array
(
[code] => PK454
[sku] => Array
(
)
[desc] => Ladies Black Hiker Boot
[description] => PK454, Ladies Black Hiker Boot
[price] => 31.00
[qty] => 1
[totalprice] => 31.00
)
)
)
)
)
)
)
Now I want to loop through that array, as I want to re-convert it into my XML format, I am able to access the first level of the array and the orderitems -> item -> code where the client purchased one single item, but I am not able to loop inside deeper level where the client purchased more than one item (second array)
here is my code
foreach($array['data']['transaction'] as $transaction){
//echo $transaction['fields']['transactionid']."<br>";
//echo $transaction['orderitems']['item']['code']."<br>";
if(isset($transaction['orderitems']['item']['code'])){
echo $transaction['orderitems']['item']['code']."<br>";
} else {
foreach($transaction['orderitems']['item'] as $multi_item){
echo "<--- MULTI ---><br>";
//tried so many things and none has worked;
//like $multi_item['code'];
}
}
}
how to access the code of each item purchased by john doe?
You can access this data by filtering the array.
In PHP >= 5.5.0 you can make a search like this;
$key = array_search('John Doe', array_column($yourArray, 'cardholdersname'));
This method will return the array that matched given criteria.
http://php.net/manual/tr/function.array-search.php

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.

Multidimensional Array Merging

I have an array that looks like this:
Array
(
[0] => Array
(
[ASINS] => Array
(
[0] => B0000714BR
[1] => B0002F6BT2
[2] => B000Y11B7G
[3] => B00571IMWK
[4] => 0767827716
)
[Product] => Array
(
[0] => Array
(
[Title] => The Best Little Whorehouse in Texas
[UPC] => 025192022029
[ASIN] => B0000714BR
[SalesRank] => 13104
[ImageURL] => http://ecx.images-amazon.com/images/I/51E0DAT5E8L._SL200_.jpg
[ProductGroup] => DVD
[Publisher] => Universal Studios
[NumberOfDiscs] => 1
[LowestPrice] => 2.91
[ShippingPrice] => 3.99
[TotalPrice] => 6.9
)
[1] => Array
(
[Title] => Are You Being Served? Again! (The Complete Series)
[UPC] => 794051202420
[ASIN] => B0002F6BT2
[SalesRank] => 13118
[ImageURL] => http://ecx.images-amazon.com/images/I/61JAD7WAT0L._SL200_.jpg
[ProductGroup] => DVD
[Publisher] => BBC Home Entertainment
[NumberOfDiscs] => 2
[LowestPrice] => 10.99
[ShippingPrice] => 3.99
[TotalPrice] => 14.98
)
[2] => Array
(
[Title] => Homeward Bound - The Incredible Journey / Homeward Bound II - Lost In San Francisco
[UPC] => 786936758320
[ASIN] => B000Y11B7G
[SalesRank] => 2145
[ImageURL] => http://ecx.images-amazon.com/images/I/61bMYabUQkL._SL200_.jpg
[ProductGroup] => DVD
[Publisher] => Walt Disney Home Entertainment
[NumberOfDiscs] => 2
[LowestPrice] => 19.95
[ShippingPrice] => 0.00
[TotalPrice] => 19.95
)
[3] => Array
(
[Title] => Joy Ride / Joy Ride 2: Dead Ahead (Two-Pack)
[UPC] => 024543647058
[ASIN] => B00571IMWK
[SalesRank] => 113077
[ImageURL] => http://ecx.images-amazon.com/images/I/5169VAQVGZL._SL200_.jpg
[ProductGroup] => DVD
[Publisher] => 20th Century Fox
[NumberOfDiscs] => 2
[LowestPrice] => 6.49
[ShippingPrice] => 3.99
[TotalPrice] => 10.48
)
[4] => Array
(
[Title] => Christine
[UPC] => 043396016194
[ASIN] => 0767827716
[SalesRank] => 40140
[ImageURL] => http://ecx.images-amazon.com/images/I/510A99AQWHL._SL200_.jpg
[ProductGroup] => DVD
[Publisher] => Sony Pictures Home Entertainment
[NumberOfDiscs] => 1
[LowestPrice] => 2.81
[ShippingPrice] => 3.99
[TotalPrice] => 6.8
)
)
)
[1] => Array
(
[ASINS] => Array
(
[0] => B00005JPH2
[1] => B00006JMRE
[2] => B000U1ZV7G
[3] => B00004RJ71
[4] => B00008977G
)
[Product] => Array
(
[0] => Array
(
[Title] => The Chronicles of Narnia: Prince Caspian
[UPC] => 786936735437
[ASIN] => B00005JPH2
[SalesRank] => 2848
[ImageURL] => http://ecx.images-amazon.com/images/I/618uTYKkadL._SL200_.jpg
[ProductGroup] => DVD
[Publisher] => Walt Disney Studios Home Entertainment
[NumberOfDiscs] => 1
[LowestPrice] => 6.62
[ShippingPrice] => 0.00
[TotalPrice] => 6.62
)
[1] => Array
(
[Title] => High Noon (Collector's Edition)
[UPC] => 017153125719
[ASIN] => B00006JMRE
[SalesRank] => 99262
[ImageURL] => http://ecx.images-amazon.com/images/I/51fPYWpMqaL._SL200_.jpg
[ProductGroup] => DVD
[Publisher] => Republic Pictures
[NumberOfDiscs] => 1
[LowestPrice] => 3.26
[ShippingPrice] => 3.99
[TotalPrice] => 7.25
)
[2] => Array
(
[Title] => 4 Film Favorites: Draculas (Dracula A.D. 1972, Dracula Has Risen from the Grave, Horror of Dracula, Taste the Blood of Dracula)
[UPC] => 085391174264
[ASIN] => B000U1ZV7G
[SalesRank] => 14994
[ImageURL] => http://ecx.images-amazon.com/images/I/61CBLkwawNL._SL200_.jpg
[ProductGroup] => DVD
[Publisher] => Warner Home Video
[NumberOfDiscs] => 2
[LowestPrice] => 6.30
[ShippingPrice] => 0.00
[TotalPrice] => 6.3
)
[3] => Array
(
[Title] => Blood In, Blood Out
[UPC] => 717951008701
[ASIN] => B00004RJ71
[SalesRank] => 1862
[ImageURL] => http://ecx.images-amazon.com/images/I/51N7R17P51L._SL200_.jpg
[ProductGroup] => DVD
[Publisher] => Hollywood Pictures Home Entertainment
[NumberOfDiscs] => 1
[LowestPrice] => 1.37
[ShippingPrice] => 3.99
[TotalPrice] => 5.36
)
[4] => Array
(
[Title] => Gone Fishin'
[UPC] => 786936182576
[ASIN] => B00008977G
[SalesRank] => 13628
[ImageURL] => http://ecx.images-amazon.com/images/I/51XQ9YR8BML._SL200_.jpg
[ProductGroup] => DVD
[Publisher] => Walt Disney Video// Mill Creek
[NumberOfDiscs] => 1
[LowestPrice] => 3.96
[ShippingPrice] => 3.99
[TotalPrice] => 7.95
)
)
)
)
Maybe I am going about this the wrong way, but I am trying to merge the outer most arrays indexed [0] and [1]. The arrays are produced from an API query. The outer most arrays indexes could continue indefinitely. I am trying to combine them so I can loop through them using a for loop to produce a new element in the inner most product arrays based on the price and sales rank in each array.
I do not even know if this is possible but any help or even a push in the right direction would be greatly appreciated.
Thanks,
Eric
$result = $array[0];
for ($i = 1; $i < count($array); $i++) {
foreach ($result as $key => &$value) {
$value = array_merge($value, $array[$i][$key]);
}
}
Now $result should be an associative array that contains the merged contents of all the original sub-arrays.

array_unique wrongly discarding values

I'm trying to use array_unique to discard of repeated values.
Array
(
[0] => Array
(
[book_id] => 1203910329
[author] => Gauci, Joe
[description] => Paperback. Very Good.
[isbn] => 9781907374067
[publisher] =>
[date] =>
[currency] => USD
[price] => 10.97
[bookseller] => xx
[bookseller_location] => GBR
[condition] => 3
[shipping_ground] => 4.73
[shipping_expedited] => 6.33
)
[1] => Array
(
[book_id] => 12312314556
[author] => Gauci, Joe
[description] => Paperback. GOOD.
[isbn] => 9781907374067
[publisher] =>
[date] =>
[currency] => USD
[price] => 1.84
[bookseller] => xx
[bookseller_location] => GBR
[condition] => 2.5
[shipping_ground] => 4.73
[shipping_expedited] => 6.33
)
[2] => Array
(
[book_id] => 12312314556
[author] => Gauci, Joe
[description] => Paperback. GOOD.
[isbn] => 9781907374067
[publisher] =>
[date] =>
[currency] => USD
[price] => 1.84
[bookseller] => xx
[bookseller_location] => GBR
[condition] => 2.5
[shipping_ground] => 4.73
[shipping_expedited] => 6.33
)
)
every time I use array_unique($results) only the first value is returned
Array
(
[0] => Array
(
[book_id] => 1203910329
[author] => Gauci, Joe
[description] => Paperback. Very Good.
[isbn] => 9781907374067
[publisher] =>
[date] =>
[currency] => USD
[price] => 10.97
[bookseller] => xx
[bookseller_location] => GBR
[condition] => 3
[shipping_ground] => 4.73
[shipping_expedited] => 6.33
)
)
Where am I going with array_unique?
You can't use array_unique with nested arrays. The comparison used is clearly documented:
(string) $elem1 === (string) $elem2

Categories