Re: restating plea for help with sort
| From: | Jason Lange | Date: | Fri, 28 Feb 2003 01:59:26 +0000 |
| Subject: | Re: restating plea for help with sort | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-137495@lists.php.net to get a copy of this message | ||
Try this code (tested):
<---------- Begin Code ---------->
foreach ($list as $key => $value) {
// If elements value is a numeral add to numeric_array
if (is_numeric($value)) {
$numeric_array[$key] = $value;
// If value is not a numeral (presumably 'n/a') add to na_array
} else {
$na_array [$key] = $value;
}
}
// Re-combine arrays placing all "other" values at the end
$final_array = array_merge($numeric_array, $na_array);
<---------- End Code ---------->
Hope that helps,
Jason
Kevin Stone wrote:
To append my own answer. In order to sort the non "n/a" values you're going to have to build a separate array and perhaps this will simplfy the problem. So instead of what I suggested below.. loop the array and start TWO new arrays within the loop. One array for "n/a" values and one for non-"n/a" values. After the values are sorted to their separate arrays do asort($nonnavals); then concatonate the two arrays together using array_merge(), putting the "n/a" array second. - Kevin ----- Original Message ----- From: "Kevin Stone" <kevin@helpelf.com> To: <jim@jimlong.net>; <php-general@lists.php.net> Sent: Thursday, February 27, 2003 12:04 PM Subject: Re: [PHP] restating plea for help with sortHmm well upon first glance looks like all you need to do is loop thatarrayand start a new array within the loop. An If/Else construct checks If the value is "n/a" then append to the back of the new array Else prepend tothe$vendor2__total,front of the new array. You append the values manually or usearray_push() and array_unshift(). Search the manual for more information. Hope that helps. - Kevin ----- Original Message ----- From: "Jim Long" <jim@jimlong.net> To: <php-general@lists.php.net> Sent: Thursday, February 27, 2003 11:35 AM Subject: [PHP] restating plea for help with sortHi, I have an array with an html link as key and different $vars as val: $list = array ( '<a href="http://someplace.com/">some place</a>' => $vendor1_total, '<a href="http://anotherplace.com/">another place</a>' =>[snip] etc.. some vals are numeric and some vals are "n/a" asort works to sort the array as a string, however, I need to sort the numeric vals numeric, then have the n/a's go away or be output at the bottom of the sort. Please be clear and gentle.. I'm not schooled in php, just a tinkerer trying to make this mess work. Thanks In Advance, Jim Long -- 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