Re: AW: [PHP] How to sort a Multidimensional array?

From: Date: Thu, 03 Mar 2005 13:11:37 +0000
Subject: Re: AW: [PHP] How to sort a Multidimensional array?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-209901@lists.php.net to get a copy of this message
Mirco Blitz wrote:
Sorry for multipost. My ISP delayed sends for about an houre. I didn't recognize it.
ok - please realise that SMTP is quite robust - mail servers will keep trying for days before giving up (in which case you will eventually get a reply saying the sending failed) - your posts will get thru just it sometimes takes a long time - the server(s?) running the php mail lists are rather overworked :-) I don't whether you're using PHP5 but if you are then here is a class that does what you want (there are a few examples at the bottom) - I won't explain how it works, because: a, the examples should tell you enough. b, it forces you to have a look at the source code :-) <?php /** * MDASort.class.php :: simple interface to sort a mutlidimensional array * * We often have arrays of arrays where the sub-arrays are rows of data * (either created or extracted from a database) - this class allows us to * easily sort arrays in the base container array by any number of keys found * in the sub-arrays. be aware that it is assumed that the array keys found in the * sub arrays are associative. Also maybe the _sortcmp method could be enhanced to * allow arbitrary levels of arrays to be sorted: by calling sort() on a level * N array also the sortKeys contents would then have to be checked to see if * they applied to the current (sub-)array *
 * @author      Some guy on PHP comment board. <http://php.net/manual/en/function.usort.php>
 * @author      Jochem Maas <jochem@iamjochem.com>
* * Apologies to 'Some guy on PHP comment board' for not being able to state his name here! */ /** * This file and its contents is not copyrighted; * The contents are free to be used by anybody under any conditions. */ class MDASort {
    private $dataArray; //the array we want to sort.
    private $sortKeys;  //the order in which we want the array to be sorted.
    function __construct()
    {
        if ($cnt = func_num_args()) {
            $args = func_get_args();
            if (isset($args[0])) {
                $this->setData($args[0]);
            }
            if (isset($args[1])) {
                $this->setSortKeys($args[1]);
            }
            if (isset($args[2]) && $args[2] === true) {
                $this->sort();
            }
        }
    }
    function _sortcmp($a, $b, $i=0)
    {
       $r = strnatcmp($a[$this->sortKeys[$i][0]],$b[$this->sortKeys[$i][0]]);
       if ($this->sortKeys[$i][1] == "DESC") $r = $r * -1;
       if($r==0) {
           $i++;
           if ($this->sortKeys[$i]) $r = $this->_sortcmp($a, $b, $i);
       }
       return $r;
    }
    function sort()
    {
       if(count($this->sortKeys)) {
           usort($this->dataArray, array($this,"_sortcmp"));
       }
    }
    function setData($dataArray = array())
    {
        $this->dataArray = $dataArray;
    }
    function setSortKeys($sortKeys = array())
    {
        $this->sortKeys = $sortKeys;
    }
    function getData()
    {
        return $this->dataArray;
    }
    function getSortKeys()
    {
        return $this->sortKeys;
    }
} /* example of usage */ /* $sorter = new MDASort; $sorter->setData( array( array("name" => "hank", "headsize" => "small", "age" => 32), array("name" => "sade", "headsize" => "petit", "age" => 36), array("name" => "hank", "headsize" => "large", "age" => 33), array("name" => "sade", "headsize" => "large", "age" => 32), array("name" => "john", "headsize" => "large", "age" => 32), array("name" => "hank", "headsize" => "small", "age" => 36), array("name" => "hank", "headsize" => "small", "age" => 40) )); $sorter->setSortKeys( array( array('name','ASC'), array('headsize','DESC'), array('age','ASC'), )); $sorter->sort(); $sortedArray = $sorter->getData(); */ /* 2nd example of usage */ /* $data = array( array("name" => "hank", "headsize" => "small", "age" => 32), array("name" => "sade", "headsize" => "petit", "age" => 36), array("name" => "hank", "headsize" => "large", "age" => 33), array("name" => "sade", "headsize" => "large", "age" => 32), array("name" => "john", "headsize" => "large", "age" => 32), array("name" => "hank", "headsize" => "small", "age" => 36), array("name" => "hank", "headsize" => "small", "age" => 40) ); $sort = array( array('name','ASC'), array('headsize','DESC'), array('age','ASC'), ); $sorter = new MDASort($data, $sort); $sorter->sort(); $sortedArray = $sorter->getData(); */ /* 3rd example of usage */ /* $data = array( array("name" => "hank", "headsize" => "small", "age" => 32), array("name" => "sade", "headsize" => "petit", "age" => 36), array("name" => "hank", "headsize" => "large", "age" => 33), array("name" => "sade", "headsize" => "large", "age" => 32), array("name" => "john", "headsize" => "large", "age" => 32), array("name" => "hank", "headsize" => "small", "age" => 36), array("name" => "hank", "headsize" => "small", "age" => 40) ); $sort = array( array('name','ASC'), array('headsize','DESC'), array('age','ASC'), ); $sorter = new MDASort($data, $sort, true); // auto sort $sortedArray = $sorter->getData(); */

« previous php.general (#209901) next »