0

Hi all,

I have a query that outputs 4 fields from a DB. Each row contains data like so:

$id = 1;
$name = John;
$items = 2;
$price = 4.2;
$saleprice = 2.0;

$id = 2;
$name = James;
$items = 1;
$price = 1.01;
$saleprice = 1.0;

$id = 3;
$name = andrew;
$items = 6;
$price = 13;
$saleprice = 10;

before I echo out the variables, I want to sort the results by the $price. I do not want to sort the results from the SQL query itself, just in PHP.

I figured I can do something like:

$testdatasort = array($id,$name,$items,$price,$saleprice);
		sort($testdatasort);
		var_dump($testdatasort);

But I just can't seem to get it to work right :(

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by chrishea
0

i dont know why you define variables like that, they share the same name, and it causes a destructive read-in, only the last input gets the value, the better way would be so store them in arrays, but this is your example ill just have to name them uniquely.

$id1 = 1;
$name1 = John;
$items1 = 2;
$price1 = 4.2;
$saleprice1 = 2.0;

$id2 = 2;
$name2 = James;
$items2 = 1;
$price2 = 1.01;
$saleprice2 = 1.0;

$id3 = 3;
$name3 = andrew;
$items3 = 6;
$price3 = 13;
$saleprice3 = 10;

then

$testdatasort = array($price1,$price2,$price3);
		sort($testdatasort);
		var_dump($testdatasort);
0

Hmmm, not quite what I'm after. the results are taken from an array, and then in a foreach function it spits out the following:

$id = $data[$market][ID][0];
$name = $data[$market][NAME][0];
$items = $data[$market][ITEMS][0];
$price = $data[$market][PRICE][0];
$saleprice = $data[$market][SALEPRICE][0];

Which is all then sent to $data variable which when called, outputs the data. But.. Before it echo's the $data from above, I want to sort the array data so it orders it by the $price from lowest to highest.

0

I think that what you need is something to sort a 2 dimensional array. I don't think that the standard sort functions do that. I had the same need a couple of years ago so I read the documentation and came up with a function that does the job. It basically sets up the data for array_multisort. It handles up to 6 sort fields.

Function array_rows_to_columns (&$array_name, $field1="", $field2="", $field3="", $field4="", $field5="", $field6="") {

	// this changes the rows to columns (ie as separate arrays) so that multisort can sort them
	// This can be used when you have a multi-dimentsion array of the form:
	//	$test_array[key][field1], $test_array[key][field2] etc
	
	// when you want to be able to do a multisort on field1, field2 and so forth.
     // This can handle up to 6 fields that are to become new arrays and sort fields.
     
     // When these are sorted, the association between the fields is retained even though they are
     // in separate arrays.
     /*
     	Example use:
		array_rows_to_columns ($test,"last_name","first_name","address","phone");
 		$result = array_multisort($sort_field1,$sort_field2,$sort_field3,$sort_field4,$test);
 		
 		where: 	$test is the original array
				last_name, first_name etc are field names to sort by
				
		It sorts the original array so you can access the results there.

		You must use "foreach .. " to extract the data - see the examples 



	*/
	
	
	global $sort_field1, $sort_field2, $sort_field3, $sort_field4, $sort_field5, $sort_field6;
     
	// --- redefine the sort arrays to get rid of any previous content ---
	$sort_field1 = array();
	$sort_field2 = array();
	$sort_field3 = array();
	$sort_field4 = array();
	$sort_field5 = array();
	$sort_field6 = array();

	foreach ($array_name as $key => $row) {
	
    		$sort_field1[$key]  		= $row[$field1];

		if ($field2 <> "") {
          	$sort_field2[$key] 		= $row[$field2];
          }

		if ($field3 <> "") {
    			$sort_field3[$key]  	= $row[$field3];
    		}

		if ($field4 <> "") {
			$sort_field4[$key]		= $row[$field4];
		}

		if ($field5 <> "") {
			$sort_field5[$key]		= $row[$field5];
		}

		if ($field6 <> "") {
			$sort_field6[$key]		= $row[$field6];
		}

	}


}

Edited by chrishea: n/a

0

forgive me, but I can't seem to get that to work :(

0

Here is an example:

<?PHP
/*
  	Demo of sorting (associative) arrays
  	
	This version uses all 4 fields as a sort key and it uses 
	a Function to convert rows to columns

*/

// ====== The Setup =========================================================================

// Note: 	You can put the include routine anywhere you want.
//        I use a global library in the root directory so I can find it
//		from any application.

$path_global = $_SERVER['DOCUMENT_ROOT']."/_include_global";

	include $path_global."/array_rows_to_columns.php";

	$test = array();

	// Note: key1, key2, key3 ... could just as well be an index number ( 0 - n).
	// It doesn't matter.
	$test['key1']['last_name'] = "Smith";
	$test['key1']['first_name'] = "John";
	$test['key1']['address'] = "123 Main";
	$test['key1']['phone'] = "905-123-1111";

	$test['key2']['last_name'] = "Smith";
	$test['key2']['first_name'] = "Al";
	$test['key2']['address'] = "555 Main";
	$test['key2']['phone'] = "905-123-1111";
	
	$test['key2a']['last_name'] = "Smith";          // same name as prev but with a different address and phone
	$test['key2a']['first_name'] = "Al";
	$test['key2a']['address'] = "111 Main";
	$test['key2a']['phone'] = "905-123-1999";

	$test['key3']['last_name'] = "Brown";
	$test['key3']['first_name'] = "Bob";
	$test['key3']['address'] = "234 Main";
	$test['key3']['phone'] = "905-123-2222";

	$test['key4']['last_name'] = "Jones";
	$test['key4']['first_name'] = "Mary";
	$test['key4']['address'] = "456 Main";
	$test['key4']['phone'] = "905-123-3333";

	$test['key5']['last_name'] = "Green";
	$test['key5']['first_name'] = "Harry";
	$test['key5']['address'] = "666 Main";
	$test['key5']['phone'] = "905-123-4444";
	
	$test['key6']['last_name'] = "Brown";
	$test['key6']['first_name'] = "Maury";
	$test['key6']['address'] = "234 Main";
	$test['key6']['phone'] = "905-123-2222";



// ======== Now we can sort ================================================================

	array_rows_to_columns ($test,"last_name","first_name","address","phone");

     // this creates arrays called sort_field1, sort_field2...
	// The working arrays are used in the multisort followed by the original array
	$result = array_multisort($sort_field1,$sort_field2,$sort_field3,$sort_field4,$test); // the original array is included last

	if ($result == false) {
		echo "<br><b>Sort Error </b>";
	}

// ======== Now we print the sorted result =================================================

	echo "<b><font size=4 color=blue>Array Sorting Demo </font></b>";
	echo "<br><br><i>The array has been sorted on last_name, first_name, address and phone.
		<br>The records started out in the order key1, key2, key2a etc.
		</i><br><br>";

	// you must use foreach to extract the info by key.
	foreach ($test as $key => $details) {
	
		echo "<br>($key) &nbsp;&nbsp;";
          echo $details['first_name'];
		echo " ".$details['last_name'];
          echo " &nbsp;&nbsp;".$details['address'];
		echo " &nbsp;&nbsp;".$details['phone'];
	}
	

?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.