Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sotring a multi-dimentional array

Status
Not open for further replies.

nesplb

Programmer
Jul 29, 2003
109
NO
Hi!

I have an array that I want to sort...
The array consist of one main-array woth sub-arrays.
The main array contains number and each number is owner of a sub array.

In this example the main array has these values:
183947, 14415, 146530
Which each ha an array of the keys: [STR], [UNIT], [PACKAGE], [NAME]

When I use the print_r command it outputs this:

Array( [183947] => Array ( [STR] => 21 [UNIT] => stk [PACKAGE] => Blisterpakning [NAME] => C FLAGYL 200 mg Tablett [FORM] => Tablett, filmdrasjert ) [14415] => Array ( [STR] => 100 [UNIT] => ml [PACKAGE] => Flaske [NAME] => A FLAGYL 40 mg/ml Mikstur [FORM] => Mikstur, suspensjon ) [146530] => Array ( [STR] => 10 [UNIT] => stk [PACKAGE] => Blisterpakning [NAME] => B FLAGYL 400 mg Tablett [FORM] => Tablett, filmdrasjert )

I now want to sort the array by the contents of [NAME], is there a way to do this? The wanted output should then be like this:

Array( [14415] => Array ( [STR] => 100 [UNIT] => ml [PACKAGE] => Flaske [NAME] => A FLAGYL 40 mg/ml Mikstur [FORM] => Mikstur, suspensjon ) [146530] => Array ( [STR] => 10 [UNIT] => stk [PACKAGE] => Blisterpakning [NAME] => B FLAGYL 400 mg Tablett [FORM] => Tablett, filmdrasjert )
[183947] => Array ( [STR] => 21 [UNIT] => stk [PACKAGE] => Blisterpakning [NAME] => C FLAGYL 200 mg Tablett [FORM] => Tablett, filmdrasjert )


In advance thanks!

Paal




Pål Nesteby

PDC-Tangen
Norway
 
it looks as though all you are trying to do is sort the array by its keys.

so ... just use ksort()
 
hmm....

but Can I use ksort to sort the array by the value [NAME]?
Perhaps I need to make a function that tests the value of [Name]?



Pål Nesteby

PDC-Tangen
Norway
 
can you be clear, please? your post make is look like you want to sort by the key of the primary array ([14415] etc).

it does not make any sense, of course to sort by this key and then by another key, as the sub-key is unique in each primary key.

if you do not want to search by the primary key but just the name value of the sub array then this will work

Code:
<?php
$array = Array( 14415 => Array ( 
									'STR' => 100, 
									'UNIT' => 'ml' ,
									'PACKAGE' => 'Flaske', 
									'NAME' =>  'A FLAGYL 40 mg/ml Mikstur', 
									'FORM' => 'Mikstur, suspensjon' ),
									
				146530 => Array ( 
									'STR' => 10, 
									'UNIT' => 'stk', 
									'PACKAGE' => 'Blisterpakning', 
									'NAME' => 'B FLAGYL 400 mg Tablett', 
									'FORM' => 'Tablett, filmdrasjert' ),
				183947 => Array ( 
									'STR' => 21, 
									'UNIT' => 'stk', 
									'PACKAGE' => 'Blisterpakning', 
									'NAME' => 'C FLAGYL 200 mg Tablett', 
									'FORM' => 'Tablett, filmdrasjert')
				);

echo "<pre>". print_r(reshapearray($array, 'NAME'), true);

function reshapeArray($array, $key){
	$_array = array();
	$return = array();
	foreach ($array as $k=>$v){
		if (isset($v[$key])){
			$_array[$v[$key]][$k] = $v;
		}
	}
	ksort($_array);
	foreach ($_array as $tmp){
		$return = $return + $tmp;
	}
	return $return;	
	
}
?>
 
Hi again!
I want to search by the primary key but just the name value of the sub array as you say in your last sentence! So your suggestion looks very good! Thank you very much!:)



Pål Nesteby

PDC-Tangen
Norway
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top