note 74725 added to function.asort
| From: | nilesh dot gamit at gmail dot com | Date: | Wed, 25 Apr 2007 10:50:58 +0000 |
| Subject: | note 74725 added to function.asort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-125851@lists.php.net to get a copy of this message | ||
function to sort 2d array: recordSort(); It is really helpful as most PHP sort functions provides
facility for sorting 1d array & multi dimensional array sorting is not handy. Normally this
function will help u.. if u r displaying some records n u want sorting by clicking on some column.
basically same is achieved by direct SQL changes like ORDER BY ASC/DESC. But in case, if your
records are not actual records, this function will help. i.e. some table has following fields in DB.
name, email, telephone and are_you_married; are_you_married is either 0 / 1. now u r displaying N
for 1 and Y for 0; and you want sorting on are_you_married on display. then ORDER BY ASC/DESC. will
not work. So, try thisâ¦
<?php
function recordSort($records, $field, $reverse, $defaultSortField = 0)
{
$uniqueSortId = 0;
$hash = array(); $sortedRecords = array(); $tempArr = array(); $indexedArray = array();
$recordArray = array();
foreach($records as $record) {
$uniqueSortId++;
$recordStr = implode("|", $record)."|".$uniqueSortId;
$recordArray[] = explode("|", $recordStr);
}
$primarySortIndex = count($record);
$records = $recordArray;
foreach($records as $record) {
$hash[$record[$primarySortIndex]] = $record[$field];
}
uasort($hash, "strnatcasecmp");
if($reverse)
$hash = array_reverse($hash, true);
$valueCount = array_count_values($hash);
foreach($hash as $primaryKey => $value) {
$indexedArray[] = $primaryKey;
}
$i = 0;
foreach($hash as $primaryKey => $value) {
$i++;
if($valueCount[$value] > 1) {
foreach($records as $record) {
if($primaryKey == $record[$primarySortIndex]) {
$tempArr[$record[$defaultSortField]."__".$i] = $record;
break;
}
}
$index = array_search($primaryKey, $indexedArray);
if( ($i == count($records)) || ($value != $hash[$indexedArray[$index+1]]) ) {
uksort($tempArr, "strnatcasecmp");
if($reverse)
$tempArr = array_reverse($tempArr);
foreach($tempArr as $newRecs) {
$sortedRecords [] = $newRecs;
}
$tempArr = array();
}
}
else {
foreach($records as $record) {
if($primaryKey == $record[$primarySortIndex]) {
$sortedRecords[] = $record;
break;
}
}
}
}
return $sortedRecords;
}
$array[0][0] = 'nilesh'; // sort_index = 0
$array[0][1] = 'yogesh'; // sort_index = 1
$array[0][2] = 'aakash'; // sort_index = 2
$array[0][3] = '100'; // sort_index = 3
$array[0][4] = 'nilesh'; // sort_index = 4
$array[0][5] = 'Nil100'; // sort_index = 5
$array[0][6] = 'Y'; // sort_index = 6
$array[1][0] = 'Nil100';
$array[1][1] = '1001';
$array[1][2] = 'nilesh';
$array[1][3] = 'nilesh';
$array[1][4] = 'nilesh';
$array[1][5] = 'yogesh';
$array[1][6] = 'Nil100';
$array[2][0] = 'Nil100';
$array[2][1] = 'Y';
$array[2][2] = '100';
$array[2][3] = '10nilesh';
$array[2][4] = 'aakash';
$array[2][5] = '_aakash';
$array[2][6] = 'aakash_';
echo "array before sorting..."
print_r($array);
/* $sortedList = recordSort(2d_array, sort_index, reverse,
second_level_sort_index_if_duplicates_found_default_is_0 = '0'); */
$sortedList = recordSort($array, 4, 0, 5);
echo "array after sorting..."
print_r($sortedList);
?>
----
Server IP: 69.39.81.135
Probable Submitter: 216.145.54.158
----
Manual Page -- http://www.php.net/manual/en/function.asort.php
Edit -- https://master.php.net/note/edit/74725
Del: integrated -- https://master.php.net/note/delete/74725/integrated
Del: useless -- https://master.php.net/note/delete/74725/useless
Del: bad code -- https://master.php.net/note/delete/74725/bad+code
Del: spam -- https://master.php.net/note/delete/74725/spam
Del: non-english -- https://master.php.net/note/delete/74725/non-english
Del: in docs -- https://master.php.net/note/delete/74725/in+docs
Del: other reasons-- https://master.php.net/note/delete/74725
Reject -- https://master.php.net/note/reject/74725
Search -- https://master.php.net/manage/user-notes.php