How to compact/rearrange a multi-dimensional array by removing null values

Viewed 181

I have an array called $rows. The first row is the header. The first column is X (input signal), the other columns are Y1,Y2,Y3, etc. (output signals). For any X value some of Y values may be NULL.

I print it with this code:

$first_key = key($rows);
foreach ($rows as $key => $row) {
    if($key == $first_key) {    // print header     
        fputcsv($out, array_keys($row));
    }
    fputcsv($out, $row);
}

The output of this code look like this (sorted by X column):

  X   |  Y1  |  Y2  | Y3 |
--------------------------
 0.1  |      |  10  |    |
 0.5  |  90  |      |  7 |
 0.7  |  15  |  40  |    |
 1.2  |      |  12  |    |

Goal: reorganize output to have columns X1,Y1,X2,Y2, etc such that in every pair (Xi,Yi) no NULL(empty) values are in between the row data and the header:

X1    |  Y1  | X2  | Y2 | X3 | Y3 |
------------------------------------------  
0.5   |  90  | 0.1 | 10 | 0.5| 7 |
0.7   |  15  | 0.7 | 40 |    |   |
      |      | 1.2 | 12 |    |   |

My attempt:

$current_header  = array_keys($rows[0]);
$size = count($current_header);
$new_header=array();   // the new header: X1, Y1, X2, Y2,...
$arr_X=array();
$arr_Y=array();
$x_column=$current_header[0];
for ($i=1; $i<$size; $i++) {
    $y_column=$current_header[$i];
    $new_header[2*$i]   = $x_column;
    $new_header[2*$i+1] = $y_column;
    $arr_Y[$y_column]   = array_column($rows, $y_column);  
    $arr_X[$y_column]   = array_column($rows, $x_column); 
}

Next step: join $arr_X[$y_column] and $arr_Y[$y_column] into arr_XY. I think in this array the key should be the index (row#); also arr_XY should not include the points where $arr_Y[$y_column] is NULL: I do not know how to do it

$arr_XY=array();
for ($i=1; $i<$size; $i++) {
    $y_column=$current_header[$i];
    // here should be the code to join arrays and eliminate NULL arr_Y points 
    $arr_XY[$y_column] = ($arr_X[$y_column], $arr_Y[$y_column]);
} 

The final step is where I need help: build and print the output rows by combining all arr_XY[$y_column] by row index.

1 Answers
Related