php - User defined sort and filtering of a string array -


lets have following data:

$data = array('av','ds','tg','ik','pk','np','rq'); 

i'm having trouble finding out tools use range of strings between 2 variables.

function sortrange ( $a, $b, $data ) {     return $range; } 

for example:

sortrange('ds','np', $data); 

would return

array('ds','tg','ik','pk','np'); 

i able backwards sorting, example:

sortrange('np','tg'); 

would return:

array('np','pk','ik','tg'); 

the closest function found might usable usort, couldn't anywhere close wanted.

thanks in advance. :)

note: following function use first occurance of $start , $end. if repeat, behavior may not expect.

function subset_range($array, $start, $end) {     $start_index = array_search($start, $array);     $end_index = array_search($end, $array);     $min = min($start_index, $end_index);     $max = max($start_index, $end_index);     $ret = array_slice($array, $min, $max - $min + 1);     return $start_index < $end_index ? $ret : array_reverse($ret); }  $data = array('av','ds','tg','ik','pk','np','rq');  print_r(subset_range($data, 'ds', 'np')); print_r(subset_range($data, 'np', 'tg')); 

output:

array (     [0] => ds     [1] => tg     [2] => ik     [3] => pk     [4] => np ) array (     [0] => np     [1] => pk     [2] => ik     [3] => tg ) 

Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -