从 .xlsx (Excel) 文件中读取数据并在数组中排列/管理或创建数组

Posted

技术标签:

【中文标题】从 .xlsx (Excel) 文件中读取数据并在数组中排列/管理或创建数组【英文标题】:Read the data from the .xlsx (Excel) file and arrange/mange in array or create an array 【发布时间】:2019-11-05 18:40:41 【问题描述】:

我想从 Excel 文件中水平和垂直读取数据并将其排列在数组中,但我可以根据以下结果/格式创建数组。

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
        )

)

【问题讨论】:

您的具体问题是什么?您是否有错误,或者您的代码的当前输出是什么导致困扰? 谢谢,@Cyril。我只想根据我已经添加到上述问题的示例数组创建一个数组。另外,我已经添加/放置了工作示例链接。 【参考方案1】:

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

<?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();
 

【讨论】:

以上是关于从 .xlsx (Excel) 文件中读取数据并在数组中排列/管理或创建数组的主要内容,如果未能解决你的问题,请参考以下文章

PHPExcel - 读取下载的 xlsx 文件

无法使用 read_excel 从 pandas 中的 xlsx 文件中读取日期列?

从Excel向MATLAB中导入数据出现了nan怎么处理

在 JavaScript/SAPUI5 中读取 Excel (xlsx) 文件数据时出现问题

从外部表中读取 Excel 文件

R读取xlsx文件