Re: sorting associative array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jeffery Fernandez wrote:
I have the following multi-dimentional array and I want to sort it by the "score" key value

...


any ideas. Thanks


A long time ago I had this problem, came up with the following class (based on someone else's stuff that I found in the comments section of the php manual) to handle it for me. its probably a rubbish way of doing it (look forward to some clever bar steward showing us how do it in 3 lines. :-) ) - there are usage examples at the bottom:

<?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://www.php.net/usort\>
 * @author      Jochem Maas <jochem@xxxxxxxxxxxxx>
 *
 * $Id: MDASort.class.php,v 1.1 2004/08/03 13:38:37 jochem Exp $
 *
 */

/**
 * 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();

*/

Jeffery


-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux