从 .xlsx (Excel) 文件和 arrange/mange 数组中读取数据或创建一个数组

Read the data from the .xlsx (Excel) file and arrange/mange in array or create an array

我想从 Excel 文件中水平和垂直读取数据并将其排列在数组中,但我可以按照下面的 result/format.[=17 创建一个数组=]

Array
(
    [ZWL] => Array
        (
            [ZWL] => 1
            [ZMW] => 2
            [ZAR] => 3
            [YER] => 4
            [XPF] => 5
        )

    [ZMW] => Array
        (
            [ZWL] => 6
            [ZMW] => 7
            [ZAR] => 8
            [YER] => 9
            [XPF] => 10
        )

    [ZAR] => Array
        (
            [ZWL] => 11
            [ZMW] => 12
            [ZAR] => 13
            [YER] => 14
            [XPF] => 15
        )

    [YER] => Array
        (
            [ZWL] => 16
            [ZMW] => 17
            [ZAR] => 18
            [YER] => 19
            [XPF] => 20
        )

    [XPF] => Array
        (
            [ZWL] => 21
            [ZMW] => 22
            [ZAR] => 23
            [YER] => 24
            [XPF] => 25
        )

    [XOF] => Array
        (
            [ZWL] => 26
            [ZMW] => 27
            [ZAR] => 28
            [YER] => 29
            [XPF] => 30
        )

    [XCD] => Array
        (
            [ZWL] => 31
            [ZMW] => 32
            [ZAR] => 33
            [YER] => 34
            [XPF] => 35
        )

    [XAF] => Array
        (
            [ZWL] => 36
            [ZMW] => 37
            [ZAR] => 38
            [YER] => 39
            [XPF] => 40
        )

    [WST] => Array
        (
            [ZWL] => 41
            [ZMW] => 42
            [ZAR] => 43
            [YER] => 44
            [XPF] => 45
        )

)

下面是我试过的代码。

<?php 
require_once ('SimpleXLSX.php');

if ($xlsx = SimpleXLSX::parse('test.xlsx')) {

    $Arr = array();
    $count = 0;
    $i = 0;
    $WithoutHeader = $xlsx->rows();
    unset($WithoutHeader[0]); 
    $totalRowInColumn = $xlsx->dimension()[1]; 
    foreach ($xlsx->rows()[0] as $key => $val) {
        if ($key != 0 && $key != 1) {
            for ($i = 1; $i < $totalRowInColumn; $i++) { 
                for ($j = 1; $j < $totalRowInColumn; $j++) { 
                    $Arr[$val][$WithoutHeader[$j][$i]] = 'val';
                }
            }
        }
    }
    echo '<pre>';
    print_r($Arr);
    echo '</pre>';
} else {
    echo SimpleXLSX::parseError();
}

function pr($data) {
    echo "<pre>";
    print_r($data);
    echo "</pre>";
}

代码示例

https://repl.it/repls/GrizzledAdventurousMicrokernel

Excel 甲酸酯

我目前的输出

Array
(
    [ZWL] => Array
        (
            [ZWL] => val
            [ZMW] => val
            [ZAR] => val
            [YER] => val
            [XPF] => val
            [1] => val
            [2] => val
            [3] => val
            [4] => val
            [5] => val
            [6] => val
            [7] => val
            [8] => val
            [9] => val
            [10] => val
            [11] => val
            [12] => val
            [13] => val
            [14] => val
            [15] => val
            [16] => val
            [17] => val
            [18] => val
            [19] => val
            [20] => val
        )

    [ZMW] => Array
        (
            [ZWL] => val
            [ZMW] => val
            [ZAR] => val
            [YER] => val
            [XPF] => val
            [1] => val
            [2] => val
            [3] => val
            [4] => val
            [5] => val
            [6] => val
            [7] => val
            [8] => val
            [9] => val
            [10] => val
            [11] => val
            [12] => val
            [13] => val
            [14] => val
            [15] => val
            [16] => val
            [17] => val
            [18] => val
            [19] => val
            [20] => val
        )

    [ZAR] => Array
        (
            [ZWL] => val
            [ZMW] => val
            [ZAR] => val
            [YER] => val
            [XPF] => val
            [1] => val
            [2] => val
            [3] => val
            [4] => val
            [5] => val
            [6] => val
            [7] => val
            [8] => val
            [9] => val
            [10] => val
            [11] => val
            [12] => val
            [13] => val
            [14] => val
            [15] => val
            [16] => val
            [17] => val
            [18] => val
            [19] => val
            [20] => val
        )

    [YER] => Array
        (
            [ZWL] => val
            [ZMW] => val
            [ZAR] => val
            [YER] => val
            [XPF] => val
            [1] => val
            [2] => val
            [3] => val
            [4] => val
            [5] => val
            [6] => val
            [7] => val
            [8] => val
            [9] => val
            [10] => val
            [11] => val
            [12] => val
            [13] => val
            [14] => val
            [15] => val
            [16] => val
            [17] => val
            [18] => val
            [19] => val
            [20] => val
        )

    [XPF] => Array
        (
            [ZWL] => val
            [ZMW] => val
            [ZAR] => val
            [YER] => val
            [XPF] => val
            [1] => val
            [2] => val
            [3] => val
            [4] => val
            [5] => val
            [6] => val
            [7] => val
            [8] => val
            [9] => val
            [10] => val
            [11] => val
            [12] => val
            [13] => val
            [14] => val
            [15] => val
            [16] => val
            [17] => val
            [18] => val
            [19] => val
            [20] => val
        )

    [XOF] => Array
        (
            [ZWL] => val
            [ZMW] => val
            [ZAR] => val
            [YER] => val
            [XPF] => val
            [1] => val
            [2] => val
            [3] => val
            [4] => val
            [5] => val
            [6] => val
            [7] => val
            [8] => val
            [9] => val
            [10] => val
            [11] => val
            [12] => val
            [13] => val
            [14] => val
            [15] => val
            [16] => val
            [17] => val
            [18] => val
            [19] => val
            [20] => val
        )

    [XCD] => Array
        (
            [ZWL] => val
            [ZMW] => val
            [ZAR] => val
            [YER] => val
            [XPF] => val
            [1] => val
            [2] => val
            [3] => val
            [4] => val
            [5] => val
            [6] => val
            [7] => val
            [8] => val
            [9] => val
            [10] => val
            [11] => val
            [12] => val
            [13] => val
            [14] => val
            [15] => val
            [16] => val
            [17] => val
            [18] => val
            [19] => val
            [20] => val
        )

    [XAF] => Array
        (
            [ZWL] => val
            [ZMW] => val
            [ZAR] => val
            [YER] => val
            [XPF] => val
            [1] => val
            [2] => val
            [3] => val
            [4] => val
            [5] => val
            [6] => val
            [7] => val
            [8] => val
            [9] => val
            [10] => val
            [11] => val
            [12] => val
            [13] => val
            [14] => val
            [15] => val
            [16] => val
            [17] => val
            [18] => val
            [19] => val
            [20] => val
        )

    [WST] => Array
        (
            [ZWL] => val
            [ZMW] => val
            [ZAR] => val
            [YER] => val
            [XPF] => val
            [1] => val
            [2] => val
            [3] => val
            [4] => val
            [5] => val
            [6] => val
            [7] => val
            [8] => val
            [9] => val
            [10] => val
            [11] => val
            [12] => val
            [13] => val
            [14] => val
            [15] => val
            [16] => val
            [17] => val
            [18] => val
            [19] => val
            [20] => val
        )

)

下面的代码,你应该尝试一次。因为我得到了你预期的结果。

<?php 
require_once ('SimpleXLSX.php');

if ($xlsx = SimpleXLSX::parse('test.xlsx')) 
{

    $first_row = []; 
    $final_arr = [];

    foreach ($xlsx->rows() as $key => $row) 
    {

      if (empty($first_row))
      {
          $first_row = array_filter($row);
      }
      else
      {

         foreach($first_row as $col_number => $col)
         {
             $final_arr[$col][$row[1]] = $row[$col_number];
         }

      }

    }

    echo '<pre>';
    print_r($final_arr);
    echo '</pre>';

} 
else 
{
    echo SimpleXLSX::parseError();
}