【问题标题】:PHP Looping array from excel来自excel的PHP循环数组
【发布时间】:2023-03-19 02:51:01
【问题描述】:

我正在尝试使用 foreach 和 for 循环获取我从 excel 上传的数据,但奇怪的是当我上传 12 行的 excel 文件时它成功插入,但是当我上传 13 行的 excel 和它只得到最后一行,我收到错误Uninitialized Offset :8

这是我上传 12 行 excel 时的数组。

array(12) 
{ 
[0]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(123) [2]=> string(5) "apple" [3]=> float(45) [4]=> string(2) "pc" [5]=> string(5) "A1011" [6]=> float(1101) [7]=> float(42110) [8]=> string(3) "asd" } } 
[1]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(2) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "asd" } } 
[2]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(20) [4]=> string(3) "box" [5]=> string(5) "A1016" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "asd" } } 
[3]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(5) "chico" [3]=> float(7) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "ads" } } 
[4]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(125) [2]=> string(9) "pineapple" [3]=> float(8) [4]=> string(3) "box" [5]=> string(5) "A1013" [6]=> float(1102) [7]=> float(42112) [8]=> string(3) "ads" } } 
[5]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(123) [2]=> string(5) "apple" [3]=> float(45) [4]=> string(2) "pc" [5]=> string(5) "A1011" [6]=> float(1101) [7]=> float(42110) [8]=> string(3) "asd" } } 
[6]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(2) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "asd" } } 
[7]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(20) [4]=> string(3) "box" [5]=> string(5) "A1016" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "asd" } } 
[8]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(5) "chico" [3]=> float(7) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "ads" } } 
[9]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(125) [2]=> string(9) "pineapple" [3]=> float(8) [4]=> string(3) "box" [5]=> string(5) "A1013" [6]=> float(1102) [7]=> float(42112) [8]=> string(3) "ads" } }
[10]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(123) [2]=> string(5) "apple" [3]=> float(45) [4]=> string(2) "pc" [5]=> string(5) "A1011" [6]=> float(1101) [7]=> float(42110) [8]=> string(3) "asd" } }
[11]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(2) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(4) "a1a1" } } }

这是 13 行时它只得到最后一行。

array(1) 
{
[0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(2) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(4) "a1a1" } }

下面是我的代码。

var_dump ($dataRow); 
        if (isset($dataRow)) {  
       echo '<pre>';
            foreach($dataRow as $key => $dataVal)   {
                foreach($dataVal as $key => $dataSume)  {
                    $fArray = $dataSume;

                    for($x=0; $x < 1; $x++ ){
                        $refs = strlen($fArray[0]);
                            $this->varOrder->addCount($fArray[0],$fArray[1],$fArray[2],$fArray[3],$fArray[4],$fArray[5],$fArray[6],$fArray[7],$fArray[8],$flag2);
                    }
                }
            }
        echo '</pre>';
         }
                $data['mode'] = 'Empty';
                $data['message'] = 'Data Successfully Uploaded!';
                $this->session->unset_userdata($dataRow);
                $this->session->unset_userdata('Count');
                $this->load->view('home_view', $data);

我的问题是为什么当我开始插入 13 行及以上时我只得到最后一行,这是我的 foreach 还是 for 循环语法?

【问题讨论】:

  • 我不完全理解你的问题。但是,至少你的 for 循环 for($x=0; $x 有点奇怪。这意味着:用 0(零)初始化 $x;只要 $x 循环只执行一次!
  • @hherger 我的问题是为什么当我开始插入 13 行及以上时我只得到最后一行,这是我的 foreach 还是 for 循环语法?
  • @hherger 我已经将 $x 加一并在 $x
  • 我添加了一个注释解决方案。请尝试提供反馈。

标签: php codeigniter


【解决方案1】:

首先,我创建了一个 $dataRow 数组,类似于您的 var_dump 显示的内容,并使用您的代码进行了尝试:结果与您所说的相同。然后我查看了 $dataRow 的结构和你的循环。

我确定您的 foreach -> foreach -> for 构造不够准确地对应于 $dataRow 的结构;这就是问题所在。
数组:$dataRow 是……

  • 数组数组
  • 从属数组只包含一项(索引=0);这些是“虚拟”数组,它们......
  • 包含一个真正的标量条目数组,即相关数据。

所以,结构是:

  • $dataRow
  •     (虚拟)数组,仅包含一个元素
  •         数据数组
  •            数据项

您可能对“虚拟”数组感到困惑。而且你用你的一次性 for(...) 循环以某种方式纠正了这个“虚拟”级别,实际上这不是必需的。

我尝试了(几乎)您的数据,16 行。它也适用于 $dataRow 数组中的任意数量的行。最外层数组和最内层数组有一个 foreach 循环。中间的虚拟数组被 foreach($dataVal[0]…) 构造覆盖。

代码

<?php

$dataRow = array(
    array( array( "CC-00057", 1, "apple", 45, "pc", "A1011", 1101, 42110, "asd" ) ),
    array( array( "CC-00057", 2, "grapes", 2, "box", 5, "A1012", 1101, 42111, "asd" ) ),
    array( array( "CC-00057", 3, "grapes", 2, "box", 5, "A1012", 1101, 42111, "asd" ) ),
    array( array( "CC-00057", 4, "grapes", 2, "box", 5, "A1012", 1101, 42111, "asd" ) ),
    array( array( "CC-00057", 5, "grapes", 2, "box", 5, "A1012", 1101, 42111, "asd" ) ),
    array( array( "CC-00057", 6, "grapes", 2, "box", 5, "A1012", 1101, 42111, "asd" ) ),
    array( array( "CC-00057", 7, "grapes", 2, "box", 5, "A1012", 1101, 42111, "asd" ) ),
    array( array( "CC-00057", 8, "grapes", 2, "box", 5, "A1012", 1101, 42111, "asd" ) ),
    array( array( "CC-00057", 9, "grapes", 2, "box", 5, "A1012", 1101, 42111, "asd" ) ),
    array( array( "CC-00057", 10, "grapes", 2, "box", 5, "A1012", 1101, 42111, "asd" ) ),
    array( array( "CC-00057", 11, "grapes", 2, "box", 5, "A1012", 1101, 42111, "asd" ) ),
    array( array( "CC-00057", 12, "grapes", 2, "box", 5, "A1012", 1101, 42111, "asd" ) ),
    array( array( "CC-00057", 13, "grapes", 2, "box", 5, "A1012", 1101, 42111, "asd" ) ),
    array( array( "CC-00057", 14, "grapes", 2, "box", 5, "A1012", 1101, 42111, "asd" ) ),
    array( array( "CC-00057", 15, "grapes", 2, "box", 5, "A1012", 1101, 42111, "asd" ) ),
    array( array( "CC-00057", 16, "grapes", 2, "box", 5, "A1012", 1101, 42111, "asd" ) ),
    array( array( "CC-00057", 17, "grapes", 2, "box", 5, "A1012", 1101, 42111, "asd" ) ),
);

echo '<h2>The $dataRow Array</h2><pre>'; var_dump ($dataRow); echo '</pre>';
if (isset($dataRow)) {  
    echo '<h2>The Result</h2><pre>';
    // Outer-most array
    foreach($dataRow as $row => $dataVal) {
        // Data arrays are in $dataVal[0], the inner-most array
        // -> Display the data array
        $res = '';
        foreach($dataVal[0] as $key => $value) {
            $res .= ((empty($res)) ? '' : ', ') . "$key=$value";
        }
        echo "Row $row: " . $res . PHP_EOL;
    }
    echo '</pre>';
}

?>

显然,如果“虚拟”数组可能有多个条目,则嵌套的 foreach 循环将如下所示:

foreach($dataRow as $row => $dataVal) {
    foreach($dataVal as $data) {
        foreach($data as $key => $value) {
            // Process the the items in the data array
        }
    }
}

结果

Row 0: 0=CC-00057, 1=1, 2=apple, 3=45, 4=pc, 5=A1011, 6=1101, 7=42110, 8=asd
Row 1: 0=CC-00057, 1=2, 2=grapes, 3=2, 4=box, 5=5, 6=A1012, 7=1101, 8=42111, 9=asd
Row 2: 0=CC-00057, 1=3, 2=grapes, 3=2, 4=box, 5=5, 6=A1012, 7=1101, 8=42111, 9=asd
Row 3: 0=CC-00057, 1=4, 2=grapes, 3=2, 4=box, 5=5, 6=A1012, 7=1101, 8=42111, 9=asd
Row 4: 0=CC-00057, 1=5, 2=grapes, 3=2, 4=box, 5=5, 6=A1012, 7=1101, 8=42111, 9=asd
Row 5: 0=CC-00057, 1=6, 2=grapes, 3=2, 4=box, 5=5, 6=A1012, 7=1101, 8=42111, 9=asd
Row 6: 0=CC-00057, 1=7, 2=grapes, 3=2, 4=box, 5=5, 6=A1012, 7=1101, 8=42111, 9=asd
Row 7: 0=CC-00057, 1=8, 2=grapes, 3=2, 4=box, 5=5, 6=A1012, 7=1101, 8=42111, 9=asd
Row 8: 0=CC-00057, 1=9, 2=grapes, 3=2, 4=box, 5=5, 6=A1012, 7=1101, 8=42111, 9=asd
Row 9: 0=CC-00057, 1=10, 2=grapes, 3=2, 4=box, 5=5, 6=A1012, 7=1101, 8=42111, 9=asd
Row 10: 0=CC-00057, 1=11, 2=grapes, 3=2, 4=box, 5=5, 6=A1012, 7=1101, 8=42111, 9=asd
Row 11: 0=CC-00057, 1=12, 2=grapes, 3=2, 4=box, 5=5, 6=A1012, 7=1101, 8=42111, 9=asd
Row 12: 0=CC-00057, 1=13, 2=grapes, 3=2, 4=box, 5=5, 6=A1012, 7=1101, 8=42111, 9=asd
Row 13: 0=CC-00057, 1=14, 2=grapes, 3=2, 4=box, 5=5, 6=A1012, 7=1101, 8=42111, 9=asd
Row 14: 0=CC-00057, 1=15, 2=grapes, 3=2, 4=box, 5=5, 6=A1012, 7=1101, 8=42111, 9=asd
Row 15: 0=CC-00057, 1=16, 2=grapes, 3=2, 4=box, 5=5, 6=A1012, 7=1101, 8=42111, 9=asd
Row 16: 0=CC-00057, 1=17, 2=grapes, 3=2, 4=box, 5=5, 6=A1012, 7=1101, 8=42111, 9=asd    

【讨论】:

    猜你喜欢
    • 2011-01-30
    • 2010-12-16
    • 2013-05-31
    • 1970-01-01
    • 2019-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多