[Solved] Php group repeated array values


One option to your expected output is to create a indexed array with the associative array below it.

This will create this kind of array:

array(4) {
  [0]=>
  array(1) {
    [203]=>
    int(1)
  }
  [1]=>
  array(1) {
    [204]=>
    int(2)
  }
  [2]=>
  array(1) {
    [203]=>
    int(2)
  }
  [3]=>
  array(1) {
    [205]=>
    int(1)
  }
}

This is not what you wanted but it is what is possible.

The code loops and keeps track of what the previous value is, if it’s the same it will count up the value, else create a new indexed and associative array with value 1.

$array =[203,204,204,203,203,205];

$i=-1;
$prev = null;
$new=[];

foreach($array as $val){
    if($val != $prev){
        $i++;
    }
    if(!isset($new[$i][$val])){
        $new[$i][$val] =1;
    }else{
        $new[$i][$val]++;
    }

    $prev = $val;
}
var_dump($new);

https://3v4l.org/W2adN

0

solved Php group repeated array values