Created
April 4, 2013 01:36
-
-
Save favila/5306999 to your computer and use it in GitHub Desktop.
`MultiIndex` class: simple class for keeping multiple indexes on an array of items with easy derived sorting. Originally written to answer [this stackoverflow question](http://stackoverflow.com/q/15800052/1002469).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MultiIndex { | |
protected $array; | |
protected $indexes = array(); | |
protected $indexdefs = array(); | |
function __construct($array, $indexdefs) | |
{ | |
$this->array = $array; | |
$this->indexdefs = $indexdefs; | |
foreach ($indexdefs as $name => $column) { | |
$this->indexes[$name] = $this->makeIndex($column); | |
} | |
} | |
function makeIndex($column) | |
{ | |
$index = array(); | |
foreach ($this->array as $k => $v) { | |
$index[$v[$column]] = $k; | |
} | |
return $index; | |
} | |
function get($key, $index=null) | |
{ | |
$datapk = ($index===null) ? $key : $this->indexes[$index][$key]; | |
return $this->array[$datapk]; | |
} | |
function getIndex($index) | |
{ | |
return $this->indexes[$index]; | |
} | |
function getData() | |
{ | |
return $this->array; | |
} | |
function indexedBy($index) | |
{ | |
$indexed = array(); | |
$indexedcolumn = $this->indexdef[$index]; | |
foreach ($this->indexes[$index] as $indexk => $arrayk) { | |
$newarray = $this->array[$arrayk]; | |
unset($newarray[$indexedcolumn]); | |
$indexed[$indexk] = $newarray; | |
} | |
return $indexed; | |
} | |
function sortedBy(/*multisort args*/) | |
/* with strings converted to arrays corresponding to index of same name */ | |
{ | |
$args = func_get_args(); | |
foreach ($args as $n => $arg) { | |
if (is_string($arg)) { | |
$args[$n] = array_keys($this->indexes[$arg]); | |
} | |
} | |
$sorted = $this->array; | |
$args[] = $sorted; | |
call_user_func_array('array_multisort', $args); | |
return $sorted; | |
} | |
} | |
/* | |
Examples: | |
$data = array( | |
array('display_order'=> 0, 'product_id'=>'samgal3', 'display_name' => 'Samsung Galaxy 3'), | |
array('display_order'=> 1, 'product_id'=>'motorazrh', 'display_name' => 'Motorola Razr HD'), | |
array('display_order'=> 2, 'product_id'=>'a', 'display_name' => 'a'), | |
array('display_order'=> 3, 'product_id'=>'c', 'display_name' => 'c'), | |
array('display_order'=> 4, 'product_id'=>'d', 'display_name' => 'd'), | |
array('display_order'=> 5, 'product_id'=>'b', 'display_name' => 'b'), | |
array('display_order'=> 6, 'product_id'=>'q', 'display_name' => 'q'), | |
array('display_order'=> 7, 'product_id'=>'f', 'display_name' => 'f'), | |
); | |
$dataidx = new MultiIndex($data, array('id'=>'product_id', 'disp'=>'display_order')); | |
var_export($dataidx->sortedBy('disp', SORT_STRING, SORT_ASC)); | |
var_export($dataidx->indexedBy('id')); | |
var_export($dataidx->get('samgal3', 'id')); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment