[solved] how to sort an array?

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
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

[solved] how to sort an array?

Post by robster »

Hi all, I have a bunch of data in an array. This first code example is inside another loop. During the loop I place the data into the array like so:

Code: Select all

if ($counter > 0)
		{
		//now stick this stuff into an array
			$trans_draw_multi_array[] = array($client_name_data_array[id], $client_full_name, $counter, $price_sub);
		}
When $counter reaches 0 the array stops and all the data has been put in the array. We then have everything we need to draw the info and I do so like this:

Code: Select all

foreach($trans_draw_multi_array as $draw) 
			{

				//work out the totals and whatnot
				$price_total = ($draw[3] + $price_total);

				$gst = ($draw[3] * $cfg_gst_rate);
				$gst = round($gst, 2);
				
				$gst_total = ($gst_total + $gst);
				$gst_total = round($gst_total);

					
				//draw the actual number and description		
				echo "<td align = \"left\" class = \"\" width = \"65%\">$draw[1]</td><td align = \"left\" class = \"\">$draw[2] sold</td><td align = \"left\" class = \"\">$$draw[3]<br>";
				if ($gst_checked == "1")
				{
					echo "<td align = \"left\" class = \"\">$$gst</td>";
				}
				else
				{
					echo "<td align = \"left\" class = \"\"></td>";
				}

			} 

	
		//draw the total
		//===============
		echo "<tr><td align = \"left\" class = \"history_dark_medium_01\" width = \"70%\">Total Transactions</td><td align = \"left\" class = \"history_dark_medium_01\">$counter_total sold</td><td align = \"left\" class = \"history_dark_medium_01\">$$price_total</td>";
		if ($gst_checked == "1")
		{
			echo "<td align = \"left\" class = \"history_dark_medium_01\">$$gst_total</td></tr>";
		}
		else
		{
			echo "<td align = \"left\" class = \"history_dark_medium_01\"></td></tr>";
		}
		echo "</table>";
That all works a treat, but unfortunately it draws each client, their total transactions, the amount they spent and GST... in the order they are in the array.

I want to be able to sort the array dynamically (via postdata say) where I let the user choose to sort by either name, transactions, or more importantly, how much each client has spent.

This is a LONG post to ask a simple question :) The question is, how can I sort this array?

Code: Select all

$trans_draw_multi_array[]
Any advice is GREATLY appreciated.


Rob
Last edited by robster on Tue Aug 29, 2006 10:34 pm, edited 1 time in total.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

array_multisort()

(this is somewhat terse, so feel free to ask for clarification)
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

Yes, I've been fumbling at http://au.php.net/array_multisort for a good hour now and am just not getting anywhere. Perhaps some guidance?

I either get corrupted data (integers appearing in my name column) or it just doesn't work... sigh :(
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Hmm, in general, if something like this is happening, I tend to think there's something fundamentally wrong (such as a poorly executed SQL query, etc). However, the trick is copying the specific array segment of the array that you're going to base the sort on (using foreach), and then performing array_multisort on that temporary array, and the whole shebang.
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

ahahahhaaaaa! now I think I get it, I'll drop back with my result :)

Thanks,

Rob
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

:(

Can you see anything wrong with this?

Code: Select all

if ($counter > 0)
		{
			//work out the full name of the client
			$client_full_name = "$client_name_data_array[name_first] $client_name_data_array[name_initial] $client_name_data_array[name_last]";
		
			//now stick this stuff into an array
			$trans_draw_multi_array[] = array($client_name_data_array[id], $client_full_name, $counter, $price_sub);
		
			$counter = "0";
			$price_sub = "0";
		}
		
	}	
	
	
	//create an array that we will sort the above array on with below.  IE:  create an array with only the price in it.
	foreach($trans_draw_multi_array as $draw) 
	{
		$trans_compare_array[] = $draw[3];
	}


	      array_multisort($trans_compare_array, SORT_ASC, SORT_REGULAR,
              $trans_draw_multi_array[3], SORT_REGULAR, SORT_DESC);

//now do a test draw to see if they're sorted or not
foreach($trans_draw_multi_array as $draw) 
				{
					echo "$draw[3], ";
				}
It gives an error on the last loop saying:

Code: Select all

Warning: array_multisort(): Array sizes are inconsistent
but still draws the contents of $draw[3] over and over, but not sorted, still in their initial order.
I'm trying, but really confused :)

Rob
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Remove the "[3]" in the multisort. On an unrelated note, quote your associative array indexes:

Code: Select all

$array['index']; // good
$array[index]; // bad!
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

oh... my... goodness.... IT WORKS!!! :)

Thanks so much, and now i've done it, I fear it not (you have no idea how long I've put this off with all the mistakes over time). I'm very fortunate you were here. Thanks so much again.

It's such a great idea, create an array you want to sort against then use the function. Love it!


:)

Rob
Post Reply