PHP get ranges of same values from array -


is there way key range of same values , make new array?

let's have array in php :

$first_array = ['1'=>'a','2'=>'a','3'=>'a','4'=>'b','5'=>'b','6'=>'a','7'=>'a']; 

how can array? there function this?

$second_array = ['1-3'=>'a','4-5'=>'b','6-7'=>'a']; 

loop through it, extract keys, generate ranges , insert new array -

$first_array = ['1'=>'a','2'=>'a','3'=>'a','4'=>'b','5'=>'b'];  $flip = array(); foreach($first_array $key => $val) {   $flip[$val][] = $key; }  $second_array = []; foreach($flip $key => $value) {     $newkey = array_shift($value).' - '.end($value);     $second_array[$newkey] = $key; } 

output

array(2) {   ["1 - 3"]=>   string(1) "a"   ["4 - 5"]=>   string(1) "b" } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -