PHP - how to flip the rows and columns of a 2D array

后端 未结 10 1371
猫巷女王i
猫巷女王i 2020-12-06 02:24

Normally I\'d be asking how to turn something like this:

1      2        3
4      5        6
7      8        9
10    11       12

Into this:

相关标签:
10条回答
  • 2020-12-06 02:56

    Just walk the array in the correct order. Assuming you have relatively small arrays, the easiest solution is just to create a brand new array during that walk.

    A solution will be of the form:

    $rows = count($arr);
    $cols = count($arr[0]); // assumes non empty matrix
    $ridx = 0;
    $cidx = 0;
    
    $out = array();
    
    foreach($arr as $rowidx => $row){
        foreach($row as $colidx => $val){
            $out[$ridx][$cidx] = $val;
            $ridx++;
            if($ridx >= $rows){
                $cidx++;
                $ridx = 0;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-06 03:01

    Got this simple one:

    $matrix = [
      [1, 2, 3],
      [1, 2, 3],
      [1, 2, 3],
    ];
    
    $flipped = array_map(function($col, $i) use($matrix){
      return array_map(function($row) use($matrix, $i){
        return $row[$i];
      }, $matrix);
    }, $matrix, array_keys($matrix));
    
    0 讨论(0)
  • 2020-12-06 03:02

    What you need is:

    function flip($array) {
        array_unshift($array, null);
        return call_user_func_array('array_map', $array);
    }
    
    0 讨论(0)
  • 2020-12-06 03:04

    here you go. It works. :)

    
    
    $input1 = array(1,2,3);
    $input2 = array(4,5,6);
    $input3 = array(7,8,9);
    $input4 = array(10,11,12);
    
    $input = array($input1,$input2,$input3,$input4);
    
    echo "\n input array";print_r($input);
    
    // flipping matrices
    $output = array();
    $intern = array();
    
    for($row=0; $row lt 4; $row++)
        for($col=0;$col lt 3;$col++)
            $intern[] = $input[$row][$col];
    
    echo "\n intern ";print_r($intern);
    
    // nesting the array
    $count = 0;
    $subcount = 0;
    
    foreach($intern as $value)
    {
    
        $output[$count][$subcount] = $value;
        $count++;
    
        if($subcount == 3)
        {
            break;
        }
    
        if($count == 4)
        {
            $count = 0;
            $subcount++;
        }
    
    
    }
    
    
    echo "\n final output ";print_r($output);
    
    0 讨论(0)
  • 2020-12-06 03:07

    Modified version of the "accepted" answer, which works MUCH better IMHO:

    function array2DFlip($arr) {
     if(!is_array($arr) || count($arr) < 1 || !isset($arr[0])) return array();
    
     $out = array();
    
     foreach($arr as $row_id => $row){
        foreach($row as $col_id => $val){
            $out[$col_id][$row_id] = $val;
        }
     }
    
     return $out;
    }
    
    0 讨论(0)
  • 2020-12-06 03:08
    function flip_row_col_array($array) {
        $out = array();
        foreach ($array as  $rowkey => $row) {
            foreach($row as $colkey => $col){
                $out[$colkey][$rowkey]=$col;
            }
        }
        return $out;
    }
    
    0 讨论(0)
提交回复
热议问题