Re: [PHP] How to sort by 3rd row in an 2d-Array
usort() /uksort()/uasort() look here : http://www.php.net/manual/en/function.uasort.php There is an example in the user notes. Andrey - Original Message - From: SED [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Friday, April 12, 2002 7:25 PM Subject: [PHP] How to sort by 3rd row in an 2d-Array Hi, I'm trying to sort an array like following myArray[1][firstname] = Joe; myArray[1][lastname] = Smith; myArray[1][company] = Bullock; myArray[1][email] = [EMAIL PROTECTED]; myArray[2][firstname] = Jim; myArray[2][lastname] = Cords; myArray[2][company] = Jamen; myArray[2][email] = [EMAIL PROTECTED]; etc... by the company name. How can I do it? I found the solution on php.net (http://www.php.net/manual/en/function.array-multisort.php) but it sorts only the first row: foreach ($myArray as $val) { $sortarray[] = $val['nafn']; echo $val['stadur']; } array_multisort($myArray, $sortarray); Thanks in advance! SED -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] How to sort by 3rd row in an 2d-Array
Hi, I'm trying to sort an array like following myArray[1][firstname] = Joe; myArray[1][lastname] = Smith; myArray[1][company] = Bullock; myArray[1][email] = [EMAIL PROTECTED]; myArray[2][firstname] = Jim; myArray[2][lastname] = Cords; myArray[2][company] = Jamen; myArray[2][email] = [EMAIL PROTECTED]; etc... by the company name. How can I do it? I found the solution on php.net (http://www.php.net/manual/en/function.array-multisort.php) but it sorts only the first row: foreach ($myArray as $val) { $sortarray[] = $val['nafn']; echo $val['stadur']; } array_multisort($myArray, $sortarray); Thanks in advance! SED -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to sort by 3rd row in an 2d-Array
This is a terribly inneficient way of handling your situation but I believe it would work. // First we're going to make a list of all company values for($i=0; $icount($myarray); $i++) { $company_values[] = $myarray[$i][company]; } // Then we're going to order the array using a standard function. natsort($company_values); // Then we're going to go through $myarray looking for each matching // value and build a new array based on the order of the sorted list. for($i=0; $icount($company_values); $i++) { for ($j=0; $j=count($myarray); $j++) { if ($company_values[$i] == $myarray[$j][company]) { $mynewarray[] = $myarray[$j]; } } } // bada bing bada boom you gots your company sorted list... I think $myarray = $mynewarray; I wrote this off the top of my head in like three minutes so any and all corrections would be most welcome. ;) -Kevin - Original Message - From: SED [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Friday, April 12, 2002 10:25 AM Subject: [PHP] How to sort by 3rd row in an 2d-Array Hi, I'm trying to sort an array like following myArray[1][firstname] = Joe; myArray[1][lastname] = Smith; myArray[1][company] = Bullock; myArray[1][email] = [EMAIL PROTECTED]; myArray[2][firstname] = Jim; myArray[2][lastname] = Cords; myArray[2][company] = Jamen; myArray[2][email] = [EMAIL PROTECTED]; etc... by the company name. How can I do it? I found the solution on php.net (http://www.php.net/manual/en/function.array-multisort.php) but it sorts only the first row: foreach ($myArray as $val) { $sortarray[] = $val['nafn']; echo $val['stadur']; } array_multisort($myArray, $sortarray); Thanks in advance! SED -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to sort by 3rd row in an 2d-Array
Or try the usort() function? ?php ... myArray[1][firstname] = Joe; myArray[1][lastname] = Smith; myArray[1][company] = Bullock; myArray[1][email] = [EMAIL PROTECTED]; myArray[2][firstname] = Jim; myArray[2][lastname] = Cords; myArray[2][company] = Jamen; myArray[2][email] = [EMAIL PROTECTED]; ... function companyCmp ($a, $b) { return strcmp($a[company],$b[company]); } usort ($myArray, companyCmp); ... ? -Steve On Friday, April 12, 2002, at 12:26 PM, Kevin Stone wrote: This is a terribly inneficient way of handling your situation but I believe it would work. // First we're going to make a list of all company values for($i=0; $icount($myarray); $i++) { $company_values[] = $myarray[$i][company]; } // Then we're going to order the array using a standard function. natsort($company_values); // Then we're going to go through $myarray looking for each matching // value and build a new array based on the order of the sorted list. for($i=0; $icount($company_values); $i++) { for ($j=0; $j=count($myarray); $j++) { if ($company_values[$i] == $myarray[$j][company]) { $mynewarray[] = $myarray[$j]; } } } // bada bing bada boom you gots your company sorted list... I think $myarray = $mynewarray; I wrote this off the top of my head in like three minutes so any and all corrections would be most welcome. ;) -Kevin - Original Message - From: SED [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Friday, April 12, 2002 10:25 AM Subject: [PHP] How to sort by 3rd row in an 2d-Array Hi, I'm trying to sort an array like following myArray[1][firstname] = Joe; myArray[1][lastname] = Smith; myArray[1][company] = Bullock; myArray[1][email] = [EMAIL PROTECTED]; myArray[2][firstname] = Jim; myArray[2][lastname] = Cords; myArray[2][company] = Jamen; myArray[2][email] = [EMAIL PROTECTED]; etc... by the company name. How can I do it? I found the solution on php.net (http://www.php.net/manual/en/function.array-multisort.php) but it sorts only the first row: foreach ($myArray as $val) { $sortarray[] = $val['nafn']; echo $val['stadur']; } array_multisort($myArray, $sortarray); Thanks in advance! SED -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php