note 89394 deleted from function.asort by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 06:48:18 +0000 |
| Subject: | note 89394 deleted from function.asort by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-196651@lists.php.net to get a copy of this message | ||
Note Submitter: Cai Black
----
I could not find a built-in to handle this specific case. I hope that someone else finds it useful
as well.
<?php
// array_sort_numerically_indexed_values
//
// (PHP 4, PHP 5)
//
// array_sort_numerically_indexed_values - Sort the numerically indexed values of an array.
//
//
// Description:
//
// bool array_sort_numerically_indexed_values ( array &$array )
//
// This function sorts the only values of the array that are numerically indexed. This is mainly
useful when sorting values that were returned from a datasource where value ordering is not
specified, is not configurable, or is unknown.
//
//
// Parameters:
//
// array
// The input array.
//
// Return Values:
//
// Returns TRUE on success or FALSE on failure (see below for more information).
//
//
// Notes:
//
// The input array MUST contain an element 'count' with an integer value representing
the number of numerically indexed array elements.
//
//
// Examples:
//
// Example #1 array_sort_numerically_indexed_values example
//
// <?php
// // An array wherein the numerically indexed elements ($pets[0] through $pets[3]) are
unordered.
// $pets=array(
// 0 => 'Biscuit',
// 1 => 'Alley',
// 2 => 'Doogie',
// 3 => 'Conan',
// 'Conan' => array(
// 0 => 'cat',
// 1 => 'male'
// ),
// 'Biscuit' => array(
// 0 => 'cat',
// 1 => 'female'
// ),
// 'Doogie' => array(
// 0 => 'dog',
// 1 => 'male'
// ),
// 'Alley' => array(
// 0 => 'dog',
// 1 => 'female'
// ),
// 'count' => 4
// );
//
// array_sort_numerically_indexed_values($pets);
//
// print_r($pets);
// ?>
//
// The above example will output:
//
// Array
// (
// [0] => Alley
// [1] => Biscuit
// [2] => Conan
// [3] => Doogie
// [Conan] => Array
// (
// [0] => cat
// [1] => male
// )
// [Biscuit] => Array
// (
// [0] => cat
// [1] => female
// )
// [Doogie] => Array
// (
// [0] => dog
// [1] => male
// )
// [Alley] => Array
// (
// [0] => dog
// [1] => female
// )
// [count] => 4
// )
//
// The numerically index array elements were sorted alphabetically.
//
// Credits:
// Cai Black < caiblack [at] hotmail [dot] com >
//
function array_sort_numerically_indexed_values(&$myArray) {
if(isset($myArray['count'])) {
$sort_again=true;
if( $sort_again && ($myArray['count'] > 1) ) {
$sort_previous_value='';
for($i=0; $i<$myArray['count']; $i++) {
$sort_again=false;
// if not the first item then
if($i > 0) {
// if current index value is less than previous value then
if($myArray[$i] < $myArray[$i-1]) {
// set swap to current index value
$sort_swap=$myArray[$i];
// set current index to previous index value
$myArray[$i]=$myArray[$i-1];
// set previous index to swap value
$myArray[$i-1]=$sort_swap;
// set $sort_again to true
$sort_again=true;
} // end if
} // end if
// set $sort_previous_value to current index value
$sort_previous_value=$myArray[$i];
} // end for
} // end if
return true;
} else {
return false;
} // end if
} // end function array_sort_numerically_indexed_values
?>