Search code examples
phparraysmultidimensional-arraymergegrouping

Group and merge array row data based on one column


I have an array a bit like:


Array (    
        [1] => Array
            (
                [1] => 21
                [2] => 3
                [0] => Analyst
            )    
        [2] => Array
            (
                [1] => 47
                [2] => 8
                [0] => Catalysis
            )

        [3] => Array
            (
                [1] => 1
                [2] => 0
                [0] => Biomaterials 
            )

        [4] => Array
            (
                [3] => 12
                [4] => 2
                [0] => Analyst
            )

       [5] => Array
            (
                [5] => 12
                [6] => 2
                [0] => Analyst
            )
...

However I would like to renumber those entries with the same [0] value so that I end up with


       [1] => Array
            (
                [1] => 21
                [2] => 3
                [3] => 12
                [4] => 2
                [5] => 12
                [6] => 2
                [0] => Analyst
            )

So far I've tried getting the [0] values out of the $results array by putting them in their own array and saying if you're already there then add [3] and [4] to where [1] and [2] are in a new array but it's not working.

$final = array();
$jArray = array();

foreach($results as $key => $result) {
  if(!in_array($result[0],$jArray) && !empty($result[0])) {
    $jArray[$i] = $result[0];
    $i++;
  }
}



for($x = 0; $x < count($results); $x++) { 
      $k = array_search($results[$x][0],$jArray);
    if(!isset($results[$x][1])) 
      $final[$k][1] = $results[$x][1];
    if(!isset($results[$x][2])) 
      $final[$k][2] = $results[$x][2];    
    if(!isset($results[$x][3])) 
      $final[$k][3] = $results[$x][3];
   if(!isset($results[$x][4])) 
      $final[$k][4] = $results[$x][4];
    if(!isset($results[$x][5]))     
      $final[$k][5] = $results[$x][5];
    if(!isset($results[$x][6]))     
      $final[$k][6] = $results[$x][6];
}

Any simpler ways of doing this?


Solution

  • You can do this way...

    $new_arr=array();
    $arkeys = array_unique(array_map(function ($v){ return $v[0];},$arr));
    foreach($arr as $k=>$arr1)
    {
                $new_arr[$arr1[0]][]=array_slice($arr1,0,count($arr1)-1);
    }
    
    foreach($arkeys as $v)
    {
    $new_arr[$v] = call_user_func_array('array_merge', $new_arr[$v]);
    }
    print_r($new_arr);
    

    OUTPUT :

    Array
    (
        [Analyst] => Array
            (
                [0] => 21
                [1] => 3
                [2] => 12
                [3] => 2
                [4] => 12
                [5] => 2
            )
    
        [Catalysis] => Array
            (
                [0] => 47
                [1] => 8
            )
    
        [Biomaterials] => Array
            (
                [0] => 1
                [1] => 0
            )
    
    )
    

    Working Demo