AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Programmieren allgemein [php] array_multisort - hat das schon mal wer benutzt?
Thema durchsuchen
Ansicht
Themen-Optionen

[php] array_multisort - hat das schon mal wer benutzt?

Ein Thema von glkgereon · begonnen am 21. Jul 2005 · letzter Beitrag vom 23. Jul 2005
 
Benutzerbild von jfheins
jfheins

Registriert seit: 10. Jun 2004
Ort: Garching (TUM)
4.579 Beiträge
 
#2

Re: [php] array_multisort - hat das schon mal wer benutzt?

  Alt 21. Jul 2005, 22:17
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.
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 15:32 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