Version: 1
Type: Function
Category: Algorithms
License: GNU General Public License
Description: sorts arrays of the form: $array_name[$some_integer][$some_key] by the 2nd key ("$some_key") for example: $userdata[1]["username"]
/*
given an array ($array), advanced_2Darray_sort will sort
an array of the following form:
$array_name[$some_integer][$some_key]
by the second key ($some_key)
executing is simple...
$messy_array[0]["firstname"] = "john";
$messy_array[0]["lastname"] = "smith";
$messy_array[1]["firstname"] = "jeff";
$messy_array[1]["lastname"] = "iscool";
$neat_array = advanced_2Darray_sort($messy_array, "firstname")
now:
$neat_array[0]["firstname"] = "jeff";
$neat_array[0]["lastname"] = "iscool";
$neat_array[1]["firstname"] = "john";
$neat_array[1]["lastname"] = "smith";
this is really useful for sorting information that
will be displayed using a table.
*/
function sys_DataCompare ($left, $right)
{
if ($left == "")
return(1);
elseif ($right == "")
return(-1);
else
{
$left = strtolower($left);
$right = strtolower($right);
}
return(strcmp($left, $right));
}
function advanced_2Darray_sort($array, $index_name)
{
$i = 0;
foreach ($array[0] as $key=>$data)
{
$sys_KEYLIST[$i] = $key;
$i++;
}
//assemble all the arrays...
for ($i = 0; $i < count($sys_KEYLIST); $i++)
{
${$sys_KEYLIST[$i]} = array();
for ($j = 0; $j < count($array); $j++)
${$sys_KEYLIST[$i]}[$j] = $array[$j][$sys_KEYLIST[$i]];
}
uasort(${$index_name}, "sys_DataCompare");
$i = 0;
foreach (${$index_name} as $sort_key=>$junk)
{
for ($j = 0; $j < count($sys_KEYLIST); $j++)
$new_array[$i][$sys_KEYLIST[$j]] = ${$sys_KEYLIST[$j][$sort_key];
$i++;
}
return($new_array);
}