![]() |
[php] array_multisort - hat das schon mal wer benutzt?
hi
es gibt in php die funktion array_multisort ![]() ich steig da nicht durch :( ich hab ein 2D-array und möchte das nach einer bestimmten spalte sortieren. so als ob ich datensätze aus ner db holen würde mit nem OrderBy dahinter... also das "Main-array" enthält ganz viele "Sub-arrays". die Subarrays enthalten daten wie zb. name, adresse, email (sind also vom aufbau immer gleich, nur die inhalte sind logischerweise immer anders) nun soll der mainarray nach name sortiert werden. ist das irgendwie zu machen? |
Re: [php] array_multisort - hat das schon mal wer benutzt?
Ich glaube nicht, ich konnte es zumindest nicht der Beschreibung entnehmen ;)
Vielleicht hilft dir der 7. Beitrag zu sort: Zitat von: hanje at hanje dot com 29-Mar-2005 12:51
Code:
During the developping of a new website, I needed a way to sort array's using a value somewhere inside a multidomensional array, while preserving the rest of the array as provided.
This is what i came up with. Comments and ideas are more then welcome as this is my first contribution to the php website. Hope it's usefull to anybody out there, could spare you some time. The comment inside the code should explain enough. You can call the function with $order being "asc" or "desc". The function: <?php function multiSortAssocR($input = array(),$priority = array(), $order = 'asc'){ $priorityKeys = array(); $ouput = array(); //$input & $priority are array's, otherwise an php error will occur. No need for checking // make tenporary array with key's and "priority" values that were found in the input array // return false on error. foreach ($input as $key => $val) if(!($priorityKeys[$key] = priorityFind(array($val),$priority))) return false; // sort the temporatry priorityarray associative, reverse/normal according to $order beeing desc/asc if ($order == 'desc') arsort($priorityKeys); else asort($priorityKeys); // everything has been done, now create output array which contains the original values of input array. // but now ordened with accoring to "priorityindex" foreach ($priorityKeys as $key => $val) $output[$key] = $input[$key]; return $output; } function priorityFind($input = array(),$priority = array()){ //$input & $priority are ought to be array's, otherwise a php error will occur // first, lets get the first key of $priority $priorityKey = key($priority); //check whether we will continue diving into the priority array if (!is_array($priority[$priorityKey])) $priorityKey = $priority[$priorityKey]; // see if we can find the key specified in $priority, in the $input array. // if not, just get the first element if (array_key_exists($priorityKey,$input)) $inputKey = $priorityKey; else $inputKey = key($input); // diving deeper into $priority and $input, let's just repeat this function till eternity and beyond (or when one of the arrays runs out) if (is_array($priority[$priorityKey])) return priorityFind($input[$inputKey],$priority[$priorityKey]); else return $input[$inputKey]; } ?> a usage example: <?php $input = array( "foo" => array( "abc" => array('123'), "pri" => array('456'), "4" ), "bar" => array( array('123'), "pri" => array('1'), "abc" ) ); $prior = array(''=>array('pri'=>array(''))); $blaat = multiSortAssocR($input,$prior); echo print_r($blaat); ?> and it's output: Array ( [bar] => Array ( [0] => Array ( [0] => 123 ) [pri] => Array ( [0] => 1 ) [1] => abc ) [foo] => Array ( [abc] => Array ( [0] => 123 ) [pri] => Array ( [0] => 456 ) [0] => 4 ) ) As you can see, it only sorts the first dimension of the array, using the key specified in $prior as sorting index. The rest of the array remains untouched. |
Re: [php] array_multisort - hat das schon mal wer benutzt?
hmm, sry, auch nach dem dritten durchlesen versteh ich noch nicht was der typ da will / geschafft hat oder so...
könnte das event. einer auf deutsch verständlich erklären, was ich da machen muss? |
Re: [php] array_multisort - hat das schon mal wer benutzt?
Salut.
Wenn ich Dich richtig verstanden habe, möchtest Du eine Datenstruktur mit folgendem Aufbau sortieren:
Delphi-Quellcode:
Das könnte so funktionieren:
type
TArrayTable = array { Zeilen } of array { Spalten } of Variant; Einerseits brauchst Du eine Funktion, die Dir sagt, wie die Relation zweier Werte ist. Wenn Du nun als Werte zwei konkrete Datensätze oder Zeilen betrachtest, wird dadurch also bestimmt, welcher der beiden Werte vor dem anderen einsortiert werden muss. Andererseits brauchst Du eine Funktion, die Dir einen solchen Vergleich auf die Elemente der zu sorierenden Liste anwendet. Deine zu sorierende Liste ist Dein Array. Daraus folgt:
Code:
Das Problem, Listen zu sortieren, nicht neu ist, daher gibt es in PHP bereits reichlich Funktionen zum Sortieren von Listen. Siehe auch: usort, uksort, asort, ... Eine weitere nützliche Funktion ist array_reverse. Für diesen Zweck eignet sich usort.
function Compare($Item1, $Item2)
{ // Vergleich hier, vergleiche Delphi-Hilfe: TList.Sort, TListSortCompare. } function MySort($List, $SortCompare) { usort($Data,$SortCompare); return($Data); } Es folgt eine Beispieldatei: Hier wird das ![]()
Code:
Die Ausgabe ist:
<?php
function MySort($Data,$SortCompare) { usort($Data,$SortCompare); return($Data); } $Futhark = array( array('ID'=>1, 'Buchstabe'=>'F', 'Bezeichnung'=>'Fehu'), array('ID'=>2, 'Buchstabe'=>'U', 'Bezeichnung'=>'Uruz'), array('ID'=>3, 'Buchstabe'=>'TH', 'Bezeichnung'=>'Thurisaz'), array('ID'=>4, 'Buchstabe'=>'A', 'Bezeichnung'=>'Ansuz'), array('ID'=>5, 'Buchstabe'=>'R', 'Bezeichnung'=>'Raidho'), array('ID'=>6, 'Buchstabe'=>'K', 'Bezeichnung'=>'Kenaz') // usw. ); ?> <h3>Das Futhark</h3> <pre> <?php print_r($Futhark); ?> </pre> <h4>Sortiert nach ID</h4> <pre> <?php function IDCompare($Item1, $Item2) { return(strnatcasecmp($Item1['ID'],$Item2['ID'])); } print_r(MySort($Futhark,'IDCompare')); ?> </pre> <h4>Sortiert nach Buchstabe</h4> <pre> <?php function BuchstabenCompare($Item1, $Item2) { return(strnatcasecmp($Item1['Buchstabe'],$Item2['Buchstabe'])); } print_r(MySort($Futhark,'BuchstabenCompare')); ?> </pre> <h4>Sortiert nach Bezeichnung</h4> <pre> <?php function BezeichnungCompare($Item1, $Item2) { return(strnatcasecmp($Item1['Bezeichnung'],$Item2['Bezeichnung'])); } print_r(MySort($Futhark,'BezeichnungCompare')); ?> </pre>
Code:
MfG
[b]Das Futhark[/b]
Array ( [0] => Array ( [ID] => 1 [Buchstabe] => F [Bezeichnung] => Fehu ) [1] => Array ( [ID] => 2 [Buchstabe] => U [Bezeichnung] => Uruz ) [2] => Array ( [ID] => 3 [Buchstabe] => TH [Bezeichnung] => Thurisaz ) [3] => Array ( [ID] => 4 [Buchstabe] => A [Bezeichnung] => Ansuz ) [4] => Array ( [ID] => 5 [Buchstabe] => R [Bezeichnung] => Raidho ) [5] => Array ( [ID] => 6 [Buchstabe] => K [Bezeichnung] => Kenaz ) ) [b]Sortiert nach ID[/b] Array ( [0] => Array ( [ID] => 1 [Buchstabe] => F [Bezeichnung] => Fehu ) [1] => Array ( [ID] => 2 [Buchstabe] => U [Bezeichnung] => Uruz ) [2] => Array ( [ID] => 3 [Buchstabe] => TH [Bezeichnung] => Thurisaz ) [3] => Array ( [ID] => 4 [Buchstabe] => A [Bezeichnung] => Ansuz ) [4] => Array ( [ID] => 5 [Buchstabe] => R [Bezeichnung] => Raidho ) [5] => Array ( [ID] => 6 [Buchstabe] => K [Bezeichnung] => Kenaz ) ) [b]Sortiert nach Buchstabe[/b] Array ( [0] => Array ( [ID] => 4 [Buchstabe] => A [Bezeichnung] => Ansuz ) [1] => Array ( [ID] => 1 [Buchstabe] => F [Bezeichnung] => Fehu ) [2] => Array ( [ID] => 6 [Buchstabe] => K [Bezeichnung] => Kenaz ) [3] => Array ( [ID] => 5 [Buchstabe] => R [Bezeichnung] => Raidho ) [4] => Array ( [ID] => 3 [Buchstabe] => TH [Bezeichnung] => Thurisaz ) [5] => Array ( [ID] => 2 [Buchstabe] => U [Bezeichnung] => Uruz ) ) [b]Sortiert nach Bezeichnung[/b] Array ( [0] => Array ( [ID] => 4 [Buchstabe] => A [Bezeichnung] => Ansuz ) [1] => Array ( [ID] => 1 [Buchstabe] => F [Bezeichnung] => Fehu ) [2] => Array ( [ID] => 6 [Buchstabe] => K [Bezeichnung] => Kenaz ) [3] => Array ( [ID] => 5 [Buchstabe] => R [Bezeichnung] => Raidho ) [4] => Array ( [ID] => 3 [Buchstabe] => TH [Bezeichnung] => Thurisaz ) [5] => Array ( [ID] => 2 [Buchstabe] => U [Bezeichnung] => Uruz ) ) Panthrax |
Alle Zeitangaben in WEZ +1. Es ist jetzt 20:15 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz