URL: https://www.overclockers.at/coding-stuff/mehrdimensionales_array_sortieren_109237/page_1 - zur Vollversion wechseln!
Moin,
ich hab mal wieder ne Frage,
Mein Beispielarray schaut so aus:
Code:$a = array( array('a' => 1, 'b' => 1, 'c' => 1, 'd' => 3), array('a' => 3, 'b' => 4, 'c' => 4, 'd' => 1), array('a' => 4, 'b' => 3, 'c' => 2, 'd' => 4), array('a' => 2, 'b' => 2, 'c' => 3, 'd' => 2) );
Code:$a = array( array('a' => 4, 'b' => 3, 'c' => 2, 'd' => 4), array('a' => 1, 'b' => 1, 'c' => 1, 'd' => 3), array('a' => 2, 'b' => 2, 'c' => 3, 'd' => 2), array('a' => 3, 'b' => 4, 'c' => 4, 'd' => 1) );
Vielleicht hilft dir folgender Text von php.net:
(http://at.php.net/manual/en/functio...y-multisort.php)
Zitatsiivv at siivv dot com
20-Oct-2003 04:23
this post expounds on that by adriano_allora at mac dot com
say u have an array like this:
$array = array("page1" => array("hits" => 1, "bytes" => 2),
"page2" => array("hits" => 2, "bytes" => 30))
and say u want to sort it based on hits, so that you can see which page had the most hits
normally this could be accomplished with uksort and a compare function, but since wrapping a compare function into a class is a problem in itself... i came up with this function, that can used as a method within the class:
function matrixSort($matrix,$sortKey) {
foreach ($this->{$matrix} as $key => $subMatrix)
$tmpArray[$key] = $subMatrix[$sortKey];
arsort($tmpArray);
$this->{$matrix} = array_merge($tmpArray,$this->{$matrix});
}
(and a version not necessarily wrapped into a class)
function matrixSort(&$matrix,$sortKey) {
foreach ($matrix as $key => $subMatrix)
$tmpArray[$key] = $subMatrix[$sortKey];
arsort($tmpArray);
return array_merge($tmpArray,$matrix);
}
this returns the same matrix form as above, but with the key "page2" now as the first element based on the sort
this solved the simple problem i was having which dissallowed me from making this process reusable within a class framework. without this i would have had to create a function, by itself, outside my class... which i considered very clunky coding
ich probiers mal aus
overclockers.at v4.thecommunity
© all rights reserved by overclockers.at 2000-2026