Array Sorting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
josephbuarao
Forum Newbie
Posts: 1
Joined: Tue Jul 13, 2010 3:16 am

Array Sorting

Post by josephbuarao »

Hi Everyone,

Im getting difficulties in sorting this array, by the way I want to sort this array according to its subarray for example
[0] => Array
(
[0] => Array
(
[0] => Are
[1] => Google
[2] => and
[3] => Zynga
[4] => Working
[5] => on
[6] => a
[7] => Gaming
[8] => Deal?
)

[1] => Array
(
[1] => Google
[4] => Working
)

[2] => Array
(
[3] => Zynga
)

[3] => Array
(
[1] => Google
)

[4] => Array
(
[1] => Google
[3] => Zynga
)

[5] => Array
(
[1] => Google
)

after sorting the output should be like this

[1] => Array
(
[0] => Array
(
[0] => Are
[1] => Google
[2] => and
[3] => Zynga
[4] => Working
[5] => on
[6] => a
[7] => Gaming
[8] => Deal?
)

[1] => Array
(
[1] => Google
[4] => Working
)


[4] => Array
(
[1] => Google
[3] => Zynga
)

[3] => Array
(
[1] => Google
)


[2] => Array
(
[3] => Zynga
)
[5] => Array
(
[1] => Google
)

In other words I want to sort this array according to its count value... Any help will be highly appreciated..
I will us this in grouping related topics in RSS feeds any tips that will make my work easily , Im going to get many RSS in different website and I will put it in one page by that I will gather all the related topics from other website... It is similar in techmeme.com ... Pls Help me...
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: Array Sorting

Post by pbs »

Hi,

I have this array sorting function, may this will help you

Code: Select all

function sort_array($array, $key, $order)
	{
		if ($order=="DESC")
			$or="arsort";
		else
			$or="asort";
		for ($i = 0; $i < sizeof($array); $i++) 
		{
			$sort_values[$i] = $array[$i][$key];
		} 
		$or($sort_values);
		reset ($sort_values);
		while (list ($arr_key, $arr_val) = each ($sort_values)) 
		{
			$sorted_arr[] = $array[$arr_key];
		}
		return $sorted_arr;
	}
Post Reply