|
Posted by Me, Myself, and I on 12/16/05 00:51
Hello all,
can someone give me an idea on how to sort a 2 dimentional array (table).
$array = array('Fruit' => 'Apple', 'Price' => 1);
$array = array('Fruit' => 'orange', 'Price' => 2);
Now this is relatively simple to do when you know the structure of the
array. However, I am trying to write a function that will sort a two
dimentional array whose structure I do not know beforehand.
based on the example below for array_multisort() on php's documentation
<?php
// Obtain a list of columns
foreach ($data as $key => $row) {
$volume[$key] = $row['volume'];
$edition[$key] = $row['edition'];
}
// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
?>
I was looking a something along these lines:
ex.
function arraysort ($array,$param){
$keylist = array_keys($array[0]);
foreach ($array as $key => $row) {
foreach($keylist as $arraykey){
$$arraykey[$key] = $row[$arraykey];
}
}
array_multisort("$param", $array);
}
arraysort($somearray,"$firstcol, SORT_DESC, $secondcol, SORT_ASC");
my first problem lies with $$arraykey[$key] and what I need is a hint on how
to create a dynamically named array.
any suggestions?
thanks,
James
Navigation:
[Reply to this message]
|