Our highest priority is to satisfy the customer through early and continuous delivery of valuable and working software.

Thursday, April 26, 2007

Sort two dimensional array in php

function recordSort()

It will sort 2d array. 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, 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…

/* function to sort 2d array, PHP sort functions i.e. sort(), ksort() etc provides facility for sorting only 1d array */

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);

2 comments: