php sort array by amount of items? - php
I have a multi-dimension array of australian states, and inside each state there are items. I was wondering if it were possible to reorder an array so the states with the most amount of items are displayed first?
Here's my array, any help would be appreciated :)
[ACT] => Array
(
[0] => stdClass Object
(
[id] => 14
[name] => Burning Log Megastore
[state] => ACT
[classid] => 1
)
[1] => stdClass Object
(
[id] => 21
[name] => Fyshwick Home & Heating
[state] => ACT
[classid] => 1
)
)
[NSW] => Array
(
[0] => stdClass Object
(
[id] => 36
[name] => A1 Hot Wood
[state] => NSW
[classid] => 11
)
[1] => stdClass Object
(
[id] => 40
[name] => Abbey Fireplaces
[state] => NSW
[classid] => 1
)
[2] => stdClass Object
(
[id] => 55
[name] => Almighty Firewood
[state] => NSW
[classid] => 11
)
[3] => stdClass Object
(
[id] => 70
[name] => Aussie Tree Services
[state] => NSW
[classid] => 11
)
[4] => stdClass Object
(
[id] => 75
[name] => B C Sands
[state] => NSW
[classid] => 11
)
[5] => stdClass Object
(
[id] => 76
[name] => B C Sands
[state] => NSW
[classid] => 11
)
[6] => stdClass Object
(
[id] => 77
[name] => B C Sands Mascot
[state] => NSW
[classid] => 11
)
[7] => stdClass Object
(
[id] => 113
[name] => Barbeques Galore (Maitland)
[state] => NSW
[classid] => 1
)
[8] => stdClass Object
(
[id] => 114
[name] => Barbeques Galore Batemans Bay
[state] => NSW
[classid] => 1
)
[9] => stdClass Object
(
[id] => 116
[name] => Barbeques Galore Wagga Wagga
[state] => NSW
[classid] => 1
)
[10] => stdClass Object
(
[id] => 131
[name] => Best Burning Fire Fuels
[state] => NSW
[classid] => 11
)
[11] => stdClass Object
(
[id] => 132
[name] => Betta Burn Firewood
[state] => NSW
[classid] => 11
)
[12] => stdClass Object
(
[id] => 136
[name] => Black Forest Firewood
[state] => NSW
[classid] => 11
)
[13] => stdClass Object
(
[id] => 181
[name] => Central Coast Brick Supplies Pty Ltd
[state] => NSW
[classid] => 1
)
[14] => stdClass Object
(
[id] => 188
[name] => Cheminee Pty Ltd
[state] => NSW
[classid] => 1
)
[15] => stdClass Object
(
[id] => 249
[name] => Embers Heating & Restoration Supplies
[state] => NSW
[classid] => 1
)
[16] => stdClass Object
(
[id] => 250
[name] => Emmbers Firewood Supplies
[state] => NSW
[classid] => 11
)
[17] => stdClass Object
(
[id] => 292
[name] => Goulburn Sand & Soil
[state] => NSW
[classid] => 11
)
[18] => stdClass Object
(
[id] => 304
[name] => Gunida Gunyah Aboriginal Corporation
[state] => NSW
[classid] => 11
)
[19] => stdClass Object
(
[id] => 334
[name] => J Perram Firewood Supplies
[state] => NSW
[classid] => 11
)
[20] => stdClass Object
(
[id] => 346
[name] => Jetmaster (Aust) Pty Ltd
[state] => NSW
[classid] => 1
)
[21] => stdClass Object
(
[id] => 402
[name] => Mark H Newton
[state] => NSW
[classid] => 4
)
[22] => stdClass Object
(
[id] => 416
[name] => Might Burn Red Gum Pty Ltd
[state] => NSW
[classid] => 11
)
[23] => stdClass Object
(
[id] => 430
[name] => Mr Stoves Pool World
[state] => NSW
[classid] => 1
)
[24] => stdClass Object
(
[id] => 455
[name] => O'Brien's Redgum Sawmills
[state] => NSW
[classid] => 11
)
[25] => stdClass Object
(
[id] => 477
[name] => PJ & CM Ducat
[state] => NSW
[classid] => 11
)
[26] => stdClass Object
(
[id] => 483
[name] => Quality Firewood All Seasons
[state] => NSW
[classid] => 11
)
[27] => stdClass Object
(
[id] => 487
[name] => R J & M E Grealy
[state] => NSW
[classid] => 11
)
[28] => stdClass Object
(
[id] => 494
[name] => RN & CA Taylor
[state] => NSW
[classid] => 11
)
[29] => stdClass Object
(
[id] => 500
[name] => Rouse Hill Firewood
[state] => NSW
[classid] => 11
)
[30] => stdClass Object
(
[id] => 504
[name] => Sapphire Coast Firewood
[state] => NSW
[classid] => 11
)
[31] => stdClass Object
(
[id] => 534
[name] => Sydney Heaters Pty Ltd
[state] => NSW
[classid] => 1
)
[32] => stdClass Object
(
[id] => 551
[name] => The Woodyard
[state] => NSW
[classid] => 11
)
[33] => stdClass Object
(
[id] => 576
[name] => W R Campi
[state] => NSW
[classid] => 11
)
[34] => stdClass Object
(
[id] => 602
[name] => Wood 4 U
[state] => NSW
[classid] => 11
)
[35] => stdClass Object
(
[id] => 603
[name] => Wood Galore
[state] => NSW
[classid] => 11
)
[36] => stdClass Object
(
[id] => 604
[name] => Wood Galore
[state] => NSW
[classid] => 11
)
)
[QLD] => Array
(
[0] => stdClass Object
(
[id] => 665
[name] => Brightspark Firewood Supplies
[state] => QLD
[classid] => 11
)
[1] => stdClass Object
(
[id] => 702
[name] => Gleno's Firewood
[state] => QLD
[classid] => 11
)
[2] => stdClass Object
(
[id] => 704
[name] => Gold Coast Fireplace & Barbeque Centre
[state] => QLD
[classid] => 1
)
[3] => stdClass Object
(
[id] => 732
[name] => Longburn Wood
[state] => QLD
[classid] => 11
)
[4] => stdClass Object
(
[id] => 742
[name] => Out In The Styx
[state] => QLD
[classid] => 11
)
[5] => stdClass Object
(
[id] => 746
[name] => Quickfire Firewood
[state] => QLD
[classid] => 11
)
)
[SA] => Array
(
[0] => stdClass Object
(
[id] => 786
[name] => Adelaide Wholesale Landscape Supplies
[state] => SA
[classid] => 11
)
[1] => stdClass Object
(
[id] => 825
[name] => Clare Wood Yard
[state] => SA
[classid] => 11
)
[2] => stdClass Object
(
[id] => 826
[name] => Clewers Electrical and Furniture
[state] => SA
[classid] => 1
)
[3] => stdClass Object
(
[id] => 832
[name] => Cullen Transport & Firewood Supplies
[state] => SA
[classid] => 11
)
[4] => stdClass Object
(
[id] => 852
[name] => Enviro Systems Renewable Resources Ltd.
[state] => SA
[classid] => 11
)
[5] => stdClass Object
(
[id] => 867
[name] => Gawler Landscaping Supplies
[state] => SA
[classid] => 11
)
[6] => stdClass Object
(
[id] => 871
[name] => Green Hornet Building & Landscape Supplies
[state] => SA
[classid] => 11
)
[7] => stdClass Object
(
[id] => 899
[name] => Mexican Living
[state] => SA
[classid] => 1
)
[8] => stdClass Object
(
[id] => 903
[name] => Mount Barker Landscape Centre
[state] => SA
[classid] => 11
)
[9] => stdClass Object
(
[id] => 911
[name] => Pecan Engineering Pty Ltd
[state] => SA
[classid] => 1
)
[10] => stdClass Object
(
[id] => 919
[name] => PW & JD Plunkett
[state] => SA
[classid] => 11
)
[11] => stdClass Object
(
[id] => 922
[name] => Renmark Firewoods & Storage Services
[state] => SA
[classid] => 11
)
[12] => stdClass Object
(
[id] => 930
[name] => South Coast Firewood (FWS Inc)
[state] => SA
[classid] => 11
)
[13] => stdClass Object
(
[id] => 937
[name] => Tinpak Trading Pty Ltd
[state] => SA
[classid] => 11
)
[14] => stdClass Object
(
[id] => 940
[name] => Traeger's Earthmoving & Transport
[state] => SA
[classid] => 11
)
[15] => stdClass Object
(
[id] => 947
[name] => Waterways Farm
[state] => SA
[classid] => 11
)
)
[TAS] => Array
(
[0] => stdClass Object
(
[id] => 985
[name] => Eastern Tiers Firewood
[state] => TAS
[classid] => 11
)
[1] => stdClass Object
(
[id] => 998
[name] => Goods Water & Garden Supplies
[state] => TAS
[classid] => 11
)
[2] => stdClass Object
(
[id] => 1011
[name] => K-Mac Wood Supplies
[state] => TAS
[classid] => 11
)
[3] => stdClass Object
(
[id] => 1019
[name] => Leslie Vale Landscape & Gravel Supplies
[state] => TAS
[classid] => 11
)
[4] => stdClass Object
(
[id] => 1023
[name] => Newmans Heating Shop
[state] => TAS
[classid] => 1
)
[5] => stdClass Object
(
[id] => 1029
[name] => Pellet Fires Tasmania
[state] => TAS
[classid] => 1
)
)
[VIC] => Array
(
[0] => stdClass Object
(
[id] => 561
[name] => TRT Pastoral Group
[state] => VIC
[classid] => 11
)
[1] => stdClass Object
(
[id] => 1059
[name] => A Grade Garden Products
[state] => VIC
[classid] => 11
)
[2] => stdClass Object
(
[id] => 1062
[name] => A.F. Gason Pty Ltd
[state] => VIC
[classid] => 1
)
[3] => stdClass Object
(
[id] => 1088
[name] => Alpine Timber
[state] => VIC
[classid] => 11
)
[4] => stdClass Object
(
[id] => 1093
[name] => Aranbe Heat
[state] => VIC
[classid] => 1
)
[5] => stdClass Object
(
[id] => 1096
[name] => ASAP Firewood
[state] => VIC
[classid] => 11
)
[6] => stdClass Object
(
[id] => 1105
[name] => Bairnsdale Stoves Heaters & BBQ's
[state] => VIC
[classid] => 1
)
[7] => stdClass Object
(
[id] => 1176
[name] => Burra Garden Supplies
[state] => VIC
[classid] => 11
)
[8] => stdClass Object
(
[id] => 1231
[name] => Daryl Fagan Firewood
[state] => VIC
[classid] => 11
)
[9] => stdClass Object
(
[id] => 1273
[name] => Firefox Industries Pty Ltd
[state] => VIC
[classid] => 1
)
[10] => stdClass Object
(
[id] => 1297
[name] => Geoffrey R Gelletly
[state] => VIC
[classid] => 4
)
[11] => stdClass Object
(
[id] => 1301
[name] => Gippsland Wood Heater Repairs
[state] => VIC
[classid] => 1
)
[12] => stdClass Object
(
[id] => 1303
[name] => Glen Dimplex Australia Pty Ltd
[state] => VIC
[classid] => 1
)
[13] => stdClass Object
(
[id] => 1306
[name] => Glowin' Firewood
[state] => VIC
[classid] => 11
)
[14] => stdClass Object
(
[id] => 1328
[name] => HRL Technology Pty Ltd
[state] => VIC
[classid] => 1
)
[15] => stdClass Object
(
[id] => 1415
[name] => Moran Logging Co Pty Ltd
[state] => VIC
[classid] => 11
)
[16] => stdClass Object
(
[id] => 1427
[name] => Murray Industries Kerang
[state] => VIC
[classid] => 11
)
[17] => stdClass Object
(
[id] => 1428
[name] => Murray Industries Swan Hill
[state] => VIC
[classid] => 4
)
[18] => stdClass Object
(
[id] => 1436
[name] => North East Fwd Strategy Implementn Comm
[state] => VIC
[classid] => 1
)
[19] => stdClass Object
(
[id] => 1438
[name] => Northern Landscape Supplies
[state] => VIC
[classid] => 11
)
[20] => stdClass Object
(
[id] => 1448
[name] => Park Orchards Garden & Building Supplies
[state] => VIC
[classid] => 11
)
[21] => stdClass Object
(
[id] => 1476
[name] => Redgumdave
[state] => VIC
[classid] => 11
)
[22] => stdClass Object
(
[id] => 1493
[name] => Sand, Soil & Rocks Pty Ltd
[state] => VIC
[classid] => 11
)
[23] => stdClass Object
(
[id] => 1495
[name] => Shamic Sheetmetal Aust Pty Ltd
[state] => VIC
[classid] => 1
)
[24] => stdClass Object
(
[id] => 1516
[name] => Surrey Hills Firewood Supplies
[state] => VIC
[classid] => 11
)
[25] => stdClass Object
(
[id] => 1529
[name] => The Flue Factory Pty Ltd
[state] => VIC
[classid] => 1
)
[26] => stdClass Object
(
[id] => 1553
[name] => Trevor King
[state] => VIC
[classid] => 11
)
[27] => stdClass Object
(
[id] => 1585
[name] => Whitlands Engineering
[state] => VIC
[classid] => 1
)
[28] => stdClass Object
(
[id] => 1591
[name] => Wignells of Melbourne Pty Ltd
[state] => VIC
[classid] => 1
)
[29] => stdClass Object
(
[id] => 1604
[name] => WWF Australia
[state] => VIC
[classid] => 1
)
[30] => stdClass Object
(
[id] => 1609
[name] => Yarra Timber Salvage
[state] => VIC
[classid] => 11
)
)
[WA] => Array
(
[0] => stdClass Object
(
[id] => 1623
[name] => All Suburbs Garden & Wood Supply
[state] => WA
[classid] => 11
)
[1] => stdClass Object
(
[id] => 1628
[name] => Barbecue Bazaar Cannington
[state] => WA
[classid] => 1
)
[2] => stdClass Object
(
[id] => 1672
[name] => European Wood Fired Ovens
[state] => WA
[classid] => 1
)
[3] => stdClass Object
(
[id] => 1681
[name] => Freo Firewood Supply
[state] => WA
[classid] => 11
)
[4] => stdClass Object
(
[id] => 1740
[name] => Roleystone Firewood
[state] => WA
[classid] => 11
)
[5] => stdClass Object
(
[id] => 1747
[name] => Smart Burn Pty Ltd
[state] => WA
[classid] => 1
)
[6] => stdClass Object
(
[id] => 1748
[name] => Specialised Tree Service
[state] => WA
[classid] => 11
)
[7] => stdClass Object
(
[id] => 1855
[name] => York Firewood & Milling
[state] => WA
[classid] => 11
)
)
)
usort($array, function($a, $b) {
return count($a) - count($b);
});
Assuming >= PHP 5.3
After running this, $arr will now be sorted from highest populated elements to lower.
function cmpArray($a, $b)
{
$countA = count($a);
$countB = count($b);
if ($countA == $countB) {
return 0;
}
return ($countA < $countB) ? 1 : -1;
}
uasort($arr, "cmpArray");
Similar to #alex's answer, but without the anonymous function (so it works on PHP <5.3)
<?php
$array = array(
'three' => array(
'one',
'two',
'three'
),
'one' => array(
'one'
),
'two' => array(
'one',
'two'
),
);
function csort($a, $b) {
return count($a) - count($b);
}
uasort($array, 'csort');
print_r($array);
?>
Output:
Array
(
[one] => Array
(
[0] => one
)
[two] => Array
(
[0] => one
[1] => two
)
[three] => Array
(
[0] => one
[1] => two
[2] => three
)
)
Related
Laravel Foreach with a multidimensional array
I'm using Laravel and I wonder how can I put this array in a foreach? for read it stdClass Object ( [data] => Array ( [0] => stdClass Object ( [id] => 0001 [type] => Item [attributes] => stdClass Object ( [product_name] => ST Slotted Sport Brake Rotors [part_number] => st126.39027SL [mfr_part_number] => 126.39027SL [part_description] => StopTech Power Slot 00-04 Volvo S40/V40 Front Left Slotted Rotor [category] => Brakes, Rotors & Pads [subcategory] => Brake Rotors - Slotted [dimensions] => Array ( [0] => stdClass Object ( [box_number] => 1 [length] => 15 [width] => 15 [height] => 4 [weight] => 12 ) ) [brand_id] => 56 [brand] => Stoptech [price_group_id] => 129 [price_group] => Stoptech [active] => 1 [regular_stock] => [dropship_controller_id] => 19 [air_freight_prohibited] => [not_carb_approved] => [prop_65] => Unknown [warehouse_availability] => Array ( [0] => stdClass Object ( [location_id] => 01 [can_place_order] => 1 ) [1] => stdClass Object ( [location_id] => 02 [can_place_order] => 1 ) [2] => stdClass Object ( [location_id] => 59 [can_place_order] => 1 ) ) [thumbnail] => https://d5otzd52uv6zz.cloudfront.net/fa33cfb5-0c91-47de-9a49-67f4175a5bff-100.jpg [barcode] => 805890204282 ) ) [1] => stdClass Object ( [id] => 078595 [type] => Item [attributes] => stdClass Object ( [product_name] => RAD Clutch Fork Stop [part_number] => rad20-0262 [mfr_part_number] => 20-0262 [part_description] => Radium Engineering Mitsubishi Evo 8-10 Clutch Fork Stop [category] => Drivetrain [subcategory] => Clutch Uncategorized [dimensions] => Array ( [0] => stdClass Object ( [box_number] => 1 [length] => 4.75 [width] => 4.5 [height] => 4.5 [weight] => 0.5 ) ) [brand_id] => 148 [brand] => Radium Engineering [price_group_id] => 406 [price_group] => Radium Engineering [active] => 1 [regular_stock] => 1 [dropship_controller_id] => 81 [air_freight_prohibited] => [not_carb_approved] => [prop_65] => Unknown [warehouse_availability] => Array ( [0] => stdClass Object ( [location_id] => 01 [can_place_order] => 1 ) [1] => stdClass Object ( [location_id] => 02 [can_place_order] => 1 ) [2] => stdClass Object ( [location_id] => 59 [can_place_order] => 1 ) ) [thumbnail] => https://d32vzsop7y1h3k.cloudfront.net/adb8244ed2d562e078c5ce2928750f2a.JPG ) ) [2] => stdClass Object ( [id] => 095906 [type] => Item [attributes] => stdClass Object ( [product_name] => MLR Trans Rebuild Kits [part_number] => mlr88075K [mfr_part_number] => 88075K [part_description] => McLeod Performance Transmission Rebuild Kit w/ Kolene Steels 4L80E 1997-2011 - Stage 1 [category] => Drivetrain [subcategory] => Transmission Rebuild Kits [dimensions] => Array ( [0] => stdClass Object ( [box_number] => 1 [length] => 24 [width] => 16 [height] => 5.25 [weight] => 14.75 ) ) [brand_id] => 169 [brand] => McLeod Racing [price_group_id] => 456 [price_group] => McLeod Racing [active] => 1 [regular_stock] => [dropship_controller_id] => 116 [air_freight_prohibited] => [not_carb_approved] => [prop_65] => Unknown [warehouse_availability] => Array ( [0] => stdClass Object ( [location_id] => 01 [can_place_order] => 1 ) [1] => stdClass Object ( [location_id] => 02 [can_place_order] => 1 ) [2] => stdClass Object ( [location_id] => 59 [can_place_order] => 1 ) ) ) ) [3] => stdClass Object ( [id] => 1 [type] => Item [attributes] => stdClass Object ( [product_name] => TXS Boost Controllers [part_number] => txs-BC-HPBC [mfr_part_number] => txs-BC-HPBC [part_description] => Turbo XS High Performance Boost Controller [category] => Forced Induction [subcategory] => Boost Controllers [dimensions] => Array ( [0] => stdClass Object ( [box_number] => 1 [length] => 4.75 [width] => 3.25 [height] => 2.25 [weight] => 0.5 ) ) [brand_id] => 63 [brand] => Turbo XS [price_group_id] => 130 [price_group] => Turbo XS [active] => 1 [regular_stock] => 1 [dropship_controller_id] => 0 [air_freight_prohibited] => [not_carb_approved] => [prop_65] => Unknown [warehouse_availability] => Array ( [0] => stdClass Object ( [location_id] => 01 [can_place_order] => 1 ) [1] => stdClass Object ( [location_id] => 02 [can_place_order] => 1 ) [2] => stdClass Object ( [location_id] => 59 [can_place_order] => 1 ) ) [thumbnail] => https://d5otzd52uv6zz.cloudfront.net/92670bde-a650-4ae6-a53e-0da8b2a26d1c-100.jpg [barcode] => 053176487038 ) ) Thanks!
Just loop through your data with foreach($yourvar->data as $key=> $data){ echo $data->id; } If you get an StdClass object it means you're iterating over an object through -> if you've an array you can print through $data['key']
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
Parsing The json decoded array in php
curl_setopt_array($ch, $curlConfig); $result = curl_exec($ch); curl_close($ch); print_r($jsonobj); //this shows me the op as: Array ( [0] => stdClass Object ( [businessId] => 6 [subscriptionId] => 6 [name] => Eazy Borehole Drillers Limited [city] => Blantyre [pin] => 3332 [region] => Southern Region [area] => City Centre [address] => P.O. Box 3332 Blantyre [email] => eazybhd#yahoo.com [website] => eazyboreholedrillers.com [district] => Blantyre [phonenumber] => 265999434445 [category] => 14 ) [1] => stdClass Object ( [businessId] => 7 [subscriptionId] => 7 [name] => Eazy Travel Limited [city] => Blantyre [pin] => 3332 [region] => Southern Region [area] => City Centre [address] => P.O. BOX 3332 Blantyre [email] => eazytravell#yahoo.com [website] => eazytravell.com [district] => Blantyre [phonenumber] => 265999434445 [category] => 15 ) [2] => stdClass Object ( [businessId] => 20 [subscriptionId] => 20 [name] => Malswitch [city] => Blantyre [pin] => 384 [region] => Southern Region [area] => City Centre [address] => PO Box 384 [email] => info#malswitch.com [website] => www.malswitch.com [district] => Blantyre [phonenumber] => 01 820 414 [category] => 69 ) [3] => stdClass Object ( [businessId] => 21 [subscriptionId] => 21 [name] => Malawi Savings Bank [city] => Blantyre [pin] => 521 [region] => Southern Region [area] => Cicty Centre [address] => PO Box 521 PO Box 521 [email] => balaka#msb.mw [website] => www.msb.mw [district] => Blantyre [phonenumber] => 01 831 016 / 01 [category] => 69 ) Now my question is how to take the inside values like [businessId],[subscriptionId] out of this so that i can use them in my html page.
you use this 'array->key'. example array->businessId; $data = Array ( [0] => stdClass Object ( [businessId] => 6 [subscriptionId] => 6 [name] => Eazy Borehole Drillers Limited [city] => Blantyre [pin] => 3332 [region] => Southern Region [area] => City Centre [address] => P.O. Box 3332 Blantyre [email] => eazybhd#yahoo.com [website] => eazyboreholedrillers.com [district] => Blantyre [phonenumber] => 265999434445 [category] => 14 ) ) $data[0]->businessId; $data[0]->subscriptionId;
Your JSON object contains objects itself, which you can access with ->. Try this: $jsonobj = ...; // Fetch your JSON object foreach($jsonobj as $item) { echo $item->businessId; // 6 echo $item->subscriptionId; // 6 echo $item->city; // Blantyre echo $item->area; // City Centre }
Sorting an array with stdClass Objects
I am attempting sort this array by each stdClass Object's [title] in an Ubercart order.tpl.php file. I have tried doing just a normal sort but since they all are stdClass Objects it doesn't do anything. <pre>Array ( [0] => stdClass Object ( [order_product_id] => 1157 [order_id] => 142 [nid] => 38 [title] => Eatonville Forest Camping Permit [manufacturer] => [model] => eatonville-camp-permit [qty] => 1 [cost] => 9.00000 [price] => 10.00000 [weight] => 0 [data] => Array ( [attributes] => Array ( ) [shippable] => 0 [restrict_qty] => 1 [module] => uc_product ) [order_uid] => 13 ) [1] => stdClass Object ( [order_product_id] => 1158 [order_id] => 142 [nid] => 35 [title] => Eatonville Forest Motorized Recreation Access Permit and Release of Liability [manufacturer] => [model] => eatonville-motor-rec-access [qty] => 1 [cost] => 175.00000 [price] => 175.00000 [weight] => 0 [data] => Array ( [attributes] => Array ( ) [shippable] => 1 [restrict_qty] => 1 [module] => uc_product ) [order_uid] => 13 ) [2] => stdClass Object ( [order_product_id] => 1159 [order_id] => 142 [nid] => 31 [title] => Snoqualmie Forest Non-motorized Recreation Access Permit for Families and Release of Liability [manufacturer] => [model] => snoqualmie-family-non-motor-rec-access [qty] => 1 [cost] => 150.00000 [price] => 150.00000 [weight] => 0 [data] => Array ( [attributes] => Array ( ) [shippable] => 1 [restrict_qty] => 1 [module] => uc_product ) [order_uid] => 13 ) [3] => stdClass Object ( [order_product_id] => 1160 [order_id] => 142 [nid] => 40 [title] => Snoqualmie Forest Camping Permit [manufacturer] => [model] => snoqualmie-camp-permit [qty] => 1 [cost] => 300.00000 [price] => 300.00000 [weight] => 0 [data] => Array ( [attributes] => Array ( ) [shippable] => 1 [restrict_qty] => 1 [module] => uc_product ) [order_uid] => 13 ) [4] => stdClass Object ( [order_product_id] => 1161 [order_id] => 142 [nid] => 39 [title] => White River Forest Camping Permit [manufacturer] => [model] => whiteriver-camp-permit [qty] => 1 [cost] => 300.00000 [price] => 300.00000 [weight] => 0 [data] => Array ( [attributes] => Array ( ) [shippable] => 1 [restrict_qty] => 1 [module] => uc_product ) [order_uid] => 13 ) [5] => stdClass Object ( [order_product_id] => 1162 [order_id] => 142 [nid] => 30 [title] => White River Forest Family Non-motorized Recreation Access Permit for Families and Release of Liability [manufacturer] => [model] => whiteriver-family-non-motor-rec-access [qty] => 1 [cost] => 150.00000 [price] => 150.00000 [weight] => 0 [data] => Array ( [attributes] => Array ( ) [shippable] => 1 [restrict_qty] => 1 [module] => uc_product ) [order_uid] => 13 ) [6] => stdClass Object ( [order_product_id] => 1163 [order_id] => 142 [nid] => 33 [title] => White River Forest Non-motorized Recreation Access Permit for Individuals and Release of Liability [manufacturer] => [model] => whiteriver-non-motor-rec-access [qty] => 1 [cost] => 75.00000 [price] => 75.00000 [weight] => 0 [data] => Array ( [attributes] => Array ( ) [shippable] => 1 [restrict_qty] => 1 [module] => uc_product ) [order_uid] => 13 ) [7] => stdClass Object ( [order_product_id] => 1164 [order_id] => 142 [nid] => 34 [title] => Snoqualmie Forest Non-motorized Recreation Access Permit for Individuals and Release of Liability [manufacturer] => [model] => snoqualmie-non-motor-rec-access [qty] => 1 [cost] => 75.00000 [price] => 75.00000 [weight] => 0 [data] => Array ( [attributes] => Array ( ) [shippable] => 1 [restrict_qty] => 1 [module] => uc_product ) [order_uid] => 13 ) )</pre> This is the code controlling it in the order.tpl.php <pre>php if (is_array($order->products)) { $context = array( 'revision' => 'formatted', 'type' => 'order_product', 'subject' => array( 'order' => $order, ), ); } </pre>
I think usort() should work: function sortByTitle($a, $b){ return strcmp($a->title, $b->title); } usort($theArray, 'sortByTitle'); You may want to throw in some checking to make sure the title member actually exists.