Sorting a multi dimentional associative array with a array key value


Here we will sort the following multi dimentional associative array with comment_date key in DESC order.

Before start your sorting, your data may look like:


 $comments = Array
   (
		[0] => Array
			(
				[id] => 10				
				[customer_id] => 2047148
				[comment] => This is a test comment.
				[comment_date] => 2011-06-10 04:52:10
			)

		[1] => Array
			(
				[id] => 8				
				[customer_id] => 2047148
				[comment] => This is a test comment.
				[comment_date] => 2011-06-10 04:49:28
			)

		[2] => Array
			(
				[id] => 51
				[customer_id] => 2047080
				[comment] => This is a test comment.
				[comment_date] => 2015-11-10 10:23:28
			)

		[3] => Array
			(
				[id] => 43437
				[customer_id] => 1
				[comment] => This is a test comment.
				[comment_date] => 2011-12-22 13:33:05
			)

	)


So you need a php function that will manage your sorting list perfectly.


	function multi_sort($array, $key, $sort_flags = SORT_REGULAR, $order = SORT_DESC) {
        if (is_array($array) && count($array) > 0) {
            if (!empty($key)) {
                $mapping = array();
                foreach ($array as $k => $v) {
                    $sort_key = '';
                    if (!is_array($key)) {
                        $sort_key = $v[$key];
                    } else {
                        foreach ($key as $key_key) {
                            $sort_key .= $v[$key_key];
                        }
                    }
                    $mapping[$k] = $sort_key;
                }
                switch ($order) {
                    case SORT_ASC:
                        asort($mapping, $sort_flags);
                        break;
                    case SORT_DESC:
                        arsort($mapping, $sort_flags);
                        break;
                }
                $sorted = array();
                foreach ($mapping as $k => $v) {
                    $sorted[] = $array[$k];
                }
                return $sorted;
            }
        }
        return $array;
     }

Now time to call the function from your code to manage your sorting.


	
    $sorted_comments = $this->multi_sort($comments, array('comment_date'));   
	

After Sorting with multi_sort function your data will look like:


	Array
	(
		[0] => Array
			(
				[id] => 51
				[customer_id] => 2047080
				[comment] => This is a test comment.
				[comment_date] => 2015-11-10 10:23:28
			)

		[1] => Array
			(
				[id] => 43437
				[customer_id] => 1
				[comment] => This is a test comment.
				[comment_date] => 2011-12-22 13:33:05
			)

		[2] => Array
			(
				[id] => 10
				[customer_id] => 2047148
				[comment] => This is a test comment.
				[comment_date] => 2011-06-10 04:52:10
			)

		[3] => Array
			(
				[id] => 8
				[customer_id] => 2047148
				[comment] => This is a test comment.
				[comment_date] => 2011-06-10 04:49:28
			)

	)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s