easyedy Posted December 28, 2007 Share Posted December 28, 2007 Hey all I've been banging my head against the wall for nearly a day trying to figure out how to sort this 3 level array. Everything I've tried has just spit out errors at me. Can you guys take a look. Here is a sample what the array outputs: $result ( [ArrayResult] => Array ( [Result] => Array ( [0]=> Array ( [iD] => x432 [Name] => Name Z [Qty] => 32 ) [1] => Array ( [iD] => x751 [Name] => Name P [Qty] => 71 ) [2] => Array ( [iD] => x915 [Name] => Name B [Qty] => 55 ) ) ) ) I'm trying to sort the array by ['Qty'] before my for each and I just can't seem to get it. Any ideas as to what I'm missing... I'm at ends wit. Thanks sort ($result['ArrayResult']['Result']['Qty']); foreach ($result['ArrayResult']['Result'] as $row) { echo $row['Qty']. ' - ' . $row['Name']; } Quote Link to comment https://forums.phpfreaks.com/topic/83487-solved-ive-spent-a-day-trying-to-figure-out-how-to-sort-this-multidimensional-array/ Share on other sites More sharing options...
Daniel0 Posted December 28, 2007 Share Posted December 28, 2007 Try this: <?php function cmp($a, $b) { if($a['Qty'] == $b['Qty']) { return 0; } return ($a['Qty'] < $b['Qty']) ? -1 : 1; } usort($result['ArrayResult'], 'cmp'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/83487-solved-ive-spent-a-day-trying-to-figure-out-how-to-sort-this-multidimensional-array/#findComment-424759 Share on other sites More sharing options...
sKunKbad Posted December 28, 2007 Share Posted December 28, 2007 Can you provide the original array? You can sort it with uasort() if Daniel0's solution isn't what you wanted. Quote Link to comment https://forums.phpfreaks.com/topic/83487-solved-ive-spent-a-day-trying-to-figure-out-how-to-sort-this-multidimensional-array/#findComment-424764 Share on other sites More sharing options...
Barand Posted December 28, 2007 Share Posted December 28, 2007 Try this: <?php function cmp($a, $b) { if($a['Qty'] == $b['Qty']) { return 0; } return ($a['Qty'] < $b['Qty']) ? -1 : 1; } usort($result['ArrayResult'], 'cmp'); ?> I think you need to sort the next level usort($result['ArrayResult']['Result'], 'cmp'); Also, FYI, the cmp() function can be reduced to <?php function cmp($a, $b) { return $a['qty'] - $b['qty']; } ?> as any -ve, 0, +ve integer result will suffice Quote Link to comment https://forums.phpfreaks.com/topic/83487-solved-ive-spent-a-day-trying-to-figure-out-how-to-sort-this-multidimensional-array/#findComment-424853 Share on other sites More sharing options...
easyedy Posted December 28, 2007 Author Share Posted December 28, 2007 Yeah Daniel0 's worked I added the next level. So it works now. Thanks. Also, thanks for the shortcut Barand. Quote Link to comment https://forums.phpfreaks.com/topic/83487-solved-ive-spent-a-day-trying-to-figure-out-how-to-sort-this-multidimensional-array/#findComment-424915 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.