[PHP] Sort multidimensional array - ARGH!
I have been messing around now for some hours and Im going mad! Im pulling some heavy data from mySQL with count and grouping, after this I have to calculate som values and then sort the outcome. Therefore Im not able to let mySQL do the accuall sort. So i stuff it into an array, and I need to sort the array and thats where the problem occures. 1. I declare an array : $stats_unsorted = array(); for loop start ... 1. fetch mySQL data 2. calculate values array_push($stats_unsorted,$sum,$user); // Eks. array_push($stats_unsorted,10,Ola); // Eks. array_push($stats_unsorted,-5,Kaare); // Eks. array_push($stats_unsorted,203,Nils); // Eks. array_push($stats_unsorted,-20,Lars); for loop end ... We now have, e.g. this : Array ( [0] = Array ( [0] = 10 [1] = Ola ) [1] = Array ( [0] = -5 [1] = Kaare ) [2] = Array ( [0] = 203 [1] = Nils ) [3] = Array ( [0] = -20 [1] = Tone ) ) - - - - - - - - - - - - - - - - - - - - Then comes the sorting part, I want to sort by this : $stats_unsorted[x][0] meaning the numbers, ofcourse I would surely like to know how to sort by the names aswell, $stats_unsorted[x][1], but I would think that is easy if I could figure out the main command to sort this in the first place. Ive tried several times with array_multisort(); but I cant get this to work, :( Help would be appretiated! $stats_SORTED = ???; Thanks in advance! -- Kim Steinhaug --- There are 10 types of people when it comes to binary numbers: those who understand them, and those who don't. --- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Sort multidimensional array - ARGH!
Hi, Thursday, February 5, 2004, 11:35:34 PM, you wrote: KS I have been messing around now for some hours and Im going mad! KS Im pulling some heavy data from mySQL with count and grouping, KS after this I have to calculate som values and then sort the outcome. KS Therefore Im not able to let mySQL do the accuall sort. KS So i stuff it into an array, and I need to sort the array and thats where KS the problem occures. KS 1. I declare an array : KS $stats_unsorted = array(); KS for loop start ... KS 1. fetch mySQL data KS 2. calculate values KS array_push($stats_unsorted,$sum,$user); KS // Eks. array_push($stats_unsorted,10,Ola); KS // Eks. array_push($stats_unsorted,-5,Kaare); KS // Eks. array_push($stats_unsorted,203,Nils); KS // Eks. array_push($stats_unsorted,-20,Lars); KS for loop end ... KS We now have, e.g. this : KS Array KS ( KS [0] = Array KS ( KS [0] = 10 KS [1] = Ola KS ) KS [1] = Array KS ( KS [0] = -5 KS [1] = Kaare KS ) KS [2] = Array KS ( KS [0] = 203 KS [1] = Nils KS ) KS [3] = Array KS ( KS [0] = -20 KS [1] = Tone KS ) KS ) KS - - - - - - - - - - - - - - - - - - - - KS Then comes the sorting part, I want to sort by this : KS $stats_unsorted[x][0] KS meaning the numbers, ofcourse I would surely like to know KS how to sort by the names aswell, $stats_unsorted[x][1], KS but I would think that is easy if I could figure out the KS main command to sort this in the first place. KS Ive tried several times with array_multisort(); but I cant get this KS to work, :( KS Help would be appretiated! KS $stats_SORTED = ???; KS Thanks in advance! KS -- KS Kim Steinhaug KS --- KS There are 10 types of people when it comes to binary numbers: KS those who understand them, and those who don't. KS --- You can use usort for multi dimension arrays: ?php function cmp($a, $b) { if ($a[0] == $b[0]) { return 0; } return ($a[0] $b[0]) ? -1 : 1; } $a = array( 0 = Array(0 = 10,1 = 'Ola'), 1 = Array(0 = -5,1 = 'Kaare'), 2 = Array(0 = 203,1 = 'Nils'), 3 = Array(0 = -20,1 = 'Tone')); usort($a, cmp); print_r($a); ? -- regards, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Sort multidimensional array using Natural-Order
Hi, I have following filename/date array Array ( [0] = Array ( [0] = 1.jpg [1] = 10.jpg [2] = 11.jpg [3] = 12.jpg [4] = 2.jpg [5] = 3.jpg [6] = 4.jpg [7] = 5.jpg [8] = 6.jpg [9] = 7.jpg [10] = 8.jpg [11] = 9.jpg ) [1] = Array ( [0] = 1026502301 [1] = 1026502312 [2] = 1026502313 [3] = 1026502550 [4] = 1026502302 [5] = 1026502304 [6] = 1026502305 [7] = 1026502619 [8] = 1026502585 [9] = 1026502308 [10] = 1026502310 [11] = 1026502311 ) ) I'm trying to sort the array by filename in Natural-Order. Array_multisort(filelist[0]),SORT_ASC,$filelist[1]) don't sort in Natural-Order usort($filelist[0],strnatcmp); Sorts only filenames, any ideas how to get dates also sorted? -Hessu- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Sort multidimensional array using Natural-Order
Hi, I have following filename/date array Array ( [0] = Array ( [0] = 1.jpg [1] = 10.jpg [2] = 11.jpg [3] = 12.jpg [4] = 2.jpg [5] = 3.jpg [6] = 4.jpg [7] = 5.jpg [8] = 6.jpg [9] = 7.jpg [10] = 8.jpg [11] = 9.jpg ) [1] = Array ( [0] = 1026502301 [1] = 1026502312 [2] = 1026502313 [3] = 1026502550 [4] = 1026502302 [5] = 1026502304 [6] = 1026502305 [7] = 1026502619 [8] = 1026502585 [9] = 1026502308 [10] = 1026502310 [11] = 1026502311 ) ) I'm trying to sort the array by filename in Natural-Order. Array_multisort(filelist[0]),SORT_ASC,$filelist[1]) don't sort in Natural-Order usort($filelist[0],strnatcmp); Sorts only filenames, any ideas how to get dates also sorted? -Hessu- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Sort multidimensional array using Natural-Order
Hi, I have following filename/date array Array ( [0] = Array ( [0] = 1.jpg [1] = 10.jpg [2] = 11.jpg [3] = 12.jpg [4] = 2.jpg [5] = 3.jpg [6] = 4.jpg [7] = 5.jpg [8] = 6.jpg [9] = 7.jpg [10] = 8.jpg [11] = 9.jpg ) [1] = Array ( [0] = 1026502301 [1] = 1026502312 [2] = 1026502313 [3] = 1026502550 [4] = 1026502302 [5] = 1026502304 [6] = 1026502305 [7] = 1026502619 [8] = 1026502585 [9] = 1026502308 [10] = 1026502310 [11] = 1026502311 ) ) I'm trying to sort the array by filename in Natural-Order. Array_multisort(filelist[0]),SORT_ASC,$filelist[1]) don't sort in Natural-Order usort($filelist[0],strnatcmp); Sorts only filenames, any ideas how to get dates also sorted? -Hessu- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] sort multidimensional array?
Hello, i have this code: while ($row = mysql_fetch_array($results)) { $tmp = get_item_reorder_priority($row["item_sku"]); $list[$r]["priority"] = $tmp[0]; $list[$r]["sku"] = $row["item_sku"]; $r++; } $list = array_multisort($list["priority"], SORT_NUMERIC, SORT_DESC); and it returns this error: Warning: Argument 1 to array_multisort() is expected to be an array or a sort flag in d://www/inventory_specific.phtml on line 109 function get_item_reorder_priority() returns an array - with $tmp[0] being an integer 1-5 $row["item_sku"] is simply a string. I'm trying to sort by priority numbers (1-5). Any help would be appreciated. -dave -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] sort multidimensional array?
while ($row = mysql_fetch_array($results)) { $tmp = get_item_reorder_priority($row["item_sku"]); $list[$r]["priority"] = $tmp[0]; $list[$r]["sku"] = $row["item_sku"]; $r++; } $list = array_multisort($list["priority"], SORT_NUMERIC, SORT_DESC); and it returns this error: Warning: Argument 1 to array_multisort() is expected to be an array or a sort flag in d://www/inventory_specific.phtml on line 109 function get_item_reorder_priority() returns an array - with $tmp[0] being an integer 1-5 $row["item_sku"] is simply a string. Umm, dunno about anybody else but it appears as if you are missing a dimension.. In the array creation: $list[$r]["priority"] is used.. emphasis on the [$r] part. When trying to re-order, you are using just $list["priority"]. missing the [$r] dimension.. bkx -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] sort multidimensional array?
I'm still confused. I'm looking to sort the list[$r] part of the array based on what value $list[$r]["priority"] is. What would the function's arguments be? ""Stuart J. Browne"" [EMAIL PROTECTED] wrote in message 99rajk$i0v$[EMAIL PROTECTED]">news:99rajk$i0v$[EMAIL PROTECTED]... while ($row = mysql_fetch_array($results)) { $tmp = get_item_reorder_priority($row["item_sku"]); $list[$r]["priority"] = $tmp[0]; $list[$r]["sku"] = $row["item_sku"]; $r++; } $list = array_multisort($list["priority"], SORT_NUMERIC, SORT_DESC); and it returns this error: Warning: Argument 1 to array_multisort() is expected to be an array or a sort flag in d://www/inventory_specific.phtml on line 109 function get_item_reorder_priority() returns an array - with $tmp[0] being an integer 1-5 $row["item_sku"] is simply a string. Umm, dunno about anybody else but it appears as if you are missing a dimension.. In the array creation: $list[$r]["priority"] is used.. emphasis on the [$r] part. When trying to re-order, you are using just $list["priority"]. missing the [$r] dimension.. bkx -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]