Chris Nizzardini, Salt Lake City Utah, Web Developer Specializing in LAMP+Ajax Since 2006

My Blog

Here is my awesome blog. You can find information on programming, linux, documentation, tips for code and database optimization, my thoughts and rants, and whatever else I feel like sharing. Feel free to contribute to the blog by posting comments and asking questions.
Programming

sorting a multidimensional array in php

This will sort a multidimensional array such as this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Array
(
    [0] => Array
        (
            [customer_ID] => 1000
            [firstName] => Chris
            [lastName] => N
            [email] => 
            [phone] => 111-111-1111
            [joined] => 2007-07-12 12:52:38
        )
 
    [1] => Array
        (
            [customer_ID] => 1001
            [firstName] => wendy
            [lastName] => p
            [email] => 
            [phone] => 111-111-1111
            [joined] => 2007-07-11 09:21:51
        )
 
    [2] => Array
        (
            [customer_ID] => 1003
            [firstName] => Lance
            [lastName] => B
            [email] => 
            [phone] => 111-111-1111
            [joined] => 2007-07-11 09:21:51
        )
)

sortBy function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	function sortBy($x, $y)
	{
		$reverse = $this->direction;
		$sort = $this->orderby;
		if($reverse=='true')
		{
			$numberToTimeBy = -1;
		}
		else
		{
			$numberToTimeBy = 1;
		}
		if(isset($sort))
		{
			if ( $x[$sort] == $y[$sort] )return 0;
			else if ( $x[$sort] < $y[$sort] ) return -1*$numberToTimeBy;
			else return 1*$numberToTimeBy;
		}
	}

Honestly, I don’t completely understand how this works. A friend at work sent me this function and I modified it a bit. Basically you need to set the $reverse variable to either true or false. This toggles the sorting from ascending to descending and vice-versa. Next set the $sort variable to the array index you want to sort by. The function gets called like this:

usort($this->dataArr,array('NameOfClass','sortBy'));

So $this->dataArr is the array you want to sort, NameOfClass is the name of the class you’re sorting in, and sortBy is the name of method within this class to call. Pretty interesting stuff huh.

Now if we were to use usort to sort the example array I gave in this post by first name we would do it like this:

1
2
3
$this->direction = 'false';
$this->orderby = 'firstName';
usort($this->dataArr,array('MyClass','sortBy'));

The following output would be given:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Array
(
    [0] => Array
        (
            [customer_ID] => 1000
            [firstName] => Chris
            [lastName] => N
            [email] => 
            [phone] => 111-111-1111
            [joined] => 2007-07-12 12:52:38
        )
 
    [1] => Array
        (
            [customer_ID] => 1003
            [firstName] => Lance
            [lastName] => B
            [email] => 
            [phone] => 111-111-1111
            [joined] => 2007-07-11 09:21:51
        )
 
    [2] => Array
        (
            [customer_ID] => 1001
            [firstName] => wendy
            [lastName] => p
            [email] => 
            [phone] => 111-111-1111
            [joined] => 2007-07-11 09:21:51
        )
)

I’ve been building a datagrid class and this has been a big help in sorting by column etc…

Related posts:

  1. ajax and the xmlhttprequest object
  2. form checkboxes in php array and storing variables in url string
  3. More JavaScript Array Methods (push and join)

Leave a Reply