this is the main.php
include("multisort.php");
//Instantiate the class
$temp_srt = new arr_multisort();
$available_srt = new arr_multisort();
//Set the array to be sorted
$temp_srt->setArray($temp_array);
$temp_srt->resetColumns();
$temp_srt->addColumn("NAME",SRT_ASC);
$temp_array = $temp_srt->sort();
for ($x=0;$x<count($temp_array);$x++)
{
formatToDisplay($temp_array[$x]);
}
function formatToDisplay($nopromo)
{
echo "<pre>";
print_r($nopromo);
echo "</pre>";
//Instantiate the class
$available_srt = new arr_multisort();
//Set the array to be sorted
$available_srt->setArray($nopromo);
$available_srt->resetColumns();
$available_srt->addColumn("AVAILABLE",SRT_ASC);
$nopromo = $available_srt->sort();
echo "<pre>";
print_r($nopromo);
echo "</pre>";
}
this is the multisort.php
class arr_multisort{
var $arr = NULL;
var $sortDef = NULL;
//Constructor
function arr_multisort(){
$this->arr = array();
$this->sortDef = array();
}
//setArray method - sets the array to be sorted
function setArray(&$arr){
$this->arr = $arr;
}
function addColumn($colName="",$colDir=SRT_ASC,$compareFunc=NULL){
$idx = $this->_getColIdx($colName);
if($idx < 0){
$this->sortDef[] = array();
$idx = count($this->sortDef)-1;
}
$this->sortDef[$idx]["colName"] = $colName;
$this->sortDef[$idx]["colDir"] = $colDir;
$this->sortDef[$idx]["compareFunc"] = $compareFunc;
}
//removeColumn method - removes entry from sorting definition
function removeColumn($colName=""){
$idx = $this->_getColIdx($colName);
if($idx >= 0) array_splice($this->sortDef,$idx,1);
}
//resetColumns - removes any columns from sorting definition. Array to sort is not affected.
function resetColumns(){
$this->sortDef = array();
}
//sort() method
function &sort(){
usort($this->arr,array($this,"_compare"));
return $this->arr;
}
//_getColIdx method [PRIVATE]
function _getColIdx($colName){
$idx = -1;
for($i=0;$i<count($this->sortDef);$i++){
$colDef = $this->sortDef[$i];
if($colDef["colName"] == $colName) $idx = $i;
}
return $idx;
}
//Comparison function [PRIVATE]
function _compare($a,$b,$idx = 0){
if(count($this->sortDef) == 0) return 0;
$colDef = $this->sortDef[$idx];
$a_cmp = $a[$colDef["colName"]];
$b_cmp = $b[$colDef["colName"]];
if(is_null($colDef["compareFunc"])){
$a_dt = strtotime($a_cmp);
$b_dt = strtotime($b_cmp);
if(($a_dt == -1) || ($b_dt == -1) || ($a_dt == false) || ($b_dt == false))
$ret = $colDef["colDir"]*strnatcasecmp($a_cmp,$b_cmp);
else{
$ret = $colDef["colDir"]*(($a_dt > $b_dt)?1:(($a_dt < $b_dt)?-1:0));
}
}
else{
$code = '$ret = ' . $colDef["compareFunc"] . '("' . $a_cmp . '","' . $b_cmp . '");';
eval($code);
$ret = $colDef["colDir"]*$ret;
}
if($ret == 0){
if($idx < (count($this->sortDef)-1))
return $this->_compare($a,$b,$idx+1);
else
return $ret;
}
else
return $ret;
}
}
my original array looks like this
Array
(
[0] => Array
(
[NAME] => HOTEL 1
[PRODUCT_ID] => 11111
[IMAGE1] =>
[ADDR] => GEYLANG
[STAR] => 2 STARS
[CHEAPEST_ROOM_PRICE] => 1
[ROOM_AVAILABILITY] => y
[NUMBER_ROOM_ROW] => 1
[ROW1] => Array
(
[PRODUCT_ID] => 11111
[NAME] => HOTEL 1
[HOTEL_CODE] =>
[AVAILABLE] => book
[TOTAL_PRICE_AFT_MARKUP] => 1
[CURRENCY] =>
[CURRENCY_CODE] => USD
[ROOM1] => Array
(
[ROOM_NAME] => Double
[MEALS] => -
[PAX_NO] => 1
[ROOM_PRICE_AFT_MARKUP] => 1
)
)
)
[63] => Array
(
[NAME] => Hotel 2
[PRODUCT_ID] => 2222
[IMAGE1] =>
[ADDR] => ERSKINE
[STAR] => 4
[CHEAPEST_ROOM_PRICE] => 1
[ROOM_AVAILABILITY] => n
[NUMBER_ROOM_ROW] => 6
[ROW1] => Array
(
[PRODUCT_ID] => 2222
[NAME] => Hotel 2
[HOTEL_CODE] =>
[AVAILABLE] => n
[TOTAL_PRICE_AFT_MARKUP] => 1
[CURRENCY] => USD
[CURRENCY_CODE] =>
[ROOM1] => Array
(
[ROOM_NAME] => Executive
[MEALS] => Included
[PAX_NO] => 1
[ROOM_PRICE_AFT_MARKUP] => 1
)
)
[ROW2] => Array
(
[PRODUCT_ID] => 2222
[NAME] => Hotel 2
[HOTEL_CODE] =>
[AVAILABLE] => n
[TOTAL_PRICE_AFT_MARKUP] => 2
[CURRENCY] => USD
[CURRENCY_CODE] =>
[ROOM1] => Array
(
[ROOM_NAME] => Executive
[MEALS] => Included
[PAX_NO] => 1
[ROOM_PRICE_AFT_MARKUP] => 2
)
)
)
)
this is what the array looks like before the class is run.
Array
(
[NAME] => HOTEL 1
[PRODUCT_ID] => 11111
[IMAGE1] =>
[ADDR] => GEYLANG
[STAR] => 2 STARS
[CHEAPEST_ROOM_PRICE] => 1
[ROOM_AVAILABILITY] => y
[NUMBER_ROOM_ROW] => 1
[ROW1] => Array
(
[PRODUCT_ID] => 11111
[NAME] => HOTEL 1
[HOTEL_CODE] =>
[AVAILABLE] => book
[TOTAL_PRICE_AFT_MARKUP] => 1
[CURRENCY] =>
[CURRENCY_CODE] => USD
[ROOM1] => Array
(
[ROOM_NAME] => Double
[MEALS] => -
[PAX_NO] => 1
[ROOM_PRICE_AFT_MARKUP] => 1
)
)
)
and this is what happened to it after the class is run!
Array
(
[0] =>
[1] => 1
[2] => GEYLANG
[3] => HOTEL 1
[4] => 2 STARS
[5] => 1
[6] => y
[7] => 11111
[8] => Array
(
[PRODUCT_ID] => 11111
[NAME] => HOTEL 1
[HOTEL_CODE] =>
[AVAILABLE] => book
[TOTAL_PRICE_AFT_MARKUP] => 1
[CURRENCY] =>
[CURRENCY_CODE] => USD
[ROOM1] => Array
(
[ROOM_NAME] => Double
[MEALS] => -
[PAX_NO] => 1
[ROOM_PRICE_AFT_MARKUP] => 1
)
)
)
it sorted the wrong column.. and messed up the array.. which leads me to conclude that the object was not called! am I right to say that? 😕