CodeIgniter AJAX Datagrid
I wrote a simple datagrid for the PHP Framework CodeIgniter. If you have questions about it this is the correct place to post comments or questions. You can find the page about the datagrid here.
Sorting Multidimensional Arrays
Sorting a two dimensional array on more then one index isn’t as complicated as it sounds. It’s also not everyday you need to do that. However, when you need to it’s useful to know how.
How To Sort A Multidimensional Array
Take the following array.
-
-
[0] =>
-
[color] => "Black"
-
[col1] => 2
-
[col2] => 2
-
[1] =>
-
[color] => "Red"
-
[col1] => 2
-
[col2] => 7
-
[2] =>
-
[color] => "Blue"
-
[col1] => 6
-
[col2] => 2
-
[3] =>
-
[color] => "Grey"
-
[col1] => 5
-
[col2] => 7
-
[4] =>
-
[color] => "Yellow"
-
[col1] => 5
-
[col2] => 4
-
[5] =>
-
[color] => "Green"
-
[col1] => 2
-
[col2] => 4
Now we want to reorder the elements in this array first by col1 then by col2 in descending order. When we are done reordering we want it to look like the following.
-
-
[0] =>
-
[color] => "Blue"
-
[col1] => 6
-
[col2] => 2
-
[1] =>
-
[color] => "Grey"
-
[col1] => 5
-
[col2] => 7[2] =>
-
[color] => "Yellow"
-
[col1] => 5
-
[col2] => 4
-
[3] =>
-
[color] => "Red"
-
[col1] => 2
-
[col2] => 7
-
[4] =>
-
[color] => "Green"
-
[col1] => 2
-
[col2] => 4
-
[5] =>
-
[color] => "Black"
-
[col1] => 2
-
[col2] => 2
How would you go about solving this problem? First you’d want to know what the PHP function usort is. Taken from php.net usort is defined as “bool usort ( array &$array , callback $cmp_function )”. This means that we pass in an array and supply a callback function. To get a full understanding of what exactly this function does and it’s draw backs visit www.php.net/usort.
Now that we know about usort we can begin writing the function.
-
-
function Sort2D($arr)
-
{
-
if ($a["col1"] == $b["col1"]) { return 0;}
-
return ($a["col1"] > $b["col1"]) ? -1 : 1;’);
-
-
if ($a["col2"] == $b["col2"]) { return 0;}
-
return ($a["col2"] > $b["col2"]) ? -1 : 1;’);
-
-
-
for ($i = 0; $i < $size; $i++) {
-
if ($arr[$i][‘col1′] != $arr[$i + 1][‘col1′]) {
-
$tmp[] = $arr[$i];
-
} else {
-
$tmp[] = $arr[$i];
-
}
-
}
-
-
return $sorted;
-
-
}
Let’s walk through the function . First you’ll notice two variables $firstSort and $subSort these are the callback functions that usort will use. Once inside the for loop you’ll see an interesting if statement that looks like “if ($arr[$i][’col1′] != $arr[$i + 1][’col1′])”. This is says if the value currently in $arr[$i][’col1′] isn’t the same as the one in front of it then we’re at the end of our current grouping. If we’re at the end of our current grouping then we need to assign the current value at $arr[$i] to our $tmp array then call usort on it. Then we need to add the contents of the $tmp to the end of our $sorted variable then unset $tmp. If we aren’t at the end of our current grouping then assign $arr[$i] to the end of $tmp.
This function is good for situations where you have a small amount of elements in your array. By small I mean around 30k. It takes roughly 3.5 seconds to 30k entries. As always feel free to leave questions about this in the comments.
Please excuse the horrible formatting of the code.
William Betts

