How to shrink / rearrange a multidimensional array by removing null values

I have an array named $rows

. The first line is the title. The first column X

(input signal), the remaining columns Y1

, Y2

, Y3

etc. (Output signals). For any value, X

some values Y

can be NULL

.

I am printing 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 result of this code looks like this (sorted by column X

):

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

      

Objective: reorganize the output to have the columns X1

, Y1

, X2

, Y2

etc., so that in each pair ( Xi

, Yi

) values NULL

(empty) between the data line and 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: attach $arr_X[$y_column]

and $arr_Y[$y_column]

to arr_XY

. I think that in this array the key should be the index (string #); also arr_XY

shouldn't include points where $arr_Y[$y_column]

NULL

: I don't know how to do this

$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]);
} 

      

In the last step, I need help: assembling and printing the output lines by concatenating everything arr_XY[$y_column]

at the line index.

+3


source to share


1 answer


Is this what you are after?

Input:

$rows=[
    ['X','Y1','Y2','Y3'],
    [.1,null,10,null],
    [.5,90,null,7],
    [.7,15,40,null],
    [1.2,null,12,null]
];

      

Method:

foreach($rows as $i=>$row){
    if(!isset($result)){                       // prepare result keys in order
        foreach(array_slice($row,1) as $col){  // don't iterate X column of header
            $no=substr($col,1);                // get number following Y
            $result["X$no"]=[];                // declare X column with column integer
            $result["Y$no"]=[];                // declare Y column with column integer
        }
    }else{
        foreach(array_slice($row,1,null,true) as $i=>$col){  // ignore X column
            if(!is_null($col)){                // only store non-null values
                $result["X$i"][]=$row[0];      // assign X value
                $result["Y$i"][]=$col;         // assign Y value
            }
        }
    }
}   
var_export($result);

      



Alternative method:

foreach($rows as $i=>$row){
    foreach(array_slice($row,1,null,true) as $c=>$col){
        if($i==0){
            $result["X$c"]=[];
            $result["Y$c"]=[];
        }elseif(!is_null($col)){
            $result["X$c"][]=$row[0];
            $result["Y$c"][]=$col;
        }
    }
}

      

Output:

array (
  'X1' => [0.5, 0.7],
  'Y1' => [90, 15],
  'X2' => [0.1, 0.7, 1.2],
  'Y2' => [10, 40, 12],
  'X3' => [0.5],
  'Y3' => [7]
];

      

+1


source







All Articles