首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >excel中的PHP循环数组

excel中的PHP循环数组
EN

Stack Overflow用户
提问于 2016-01-25 20:41:39
回答 1查看 157关注 0票数 0

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

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

代码语言:javascript
运行
复制
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行时,它只得到最后一行。

代码语言:javascript
运行
复制
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" } }

下面是我的代码。

代码语言:javascript
运行
复制
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循环语法上吗?

EN

回答 1

Stack Overflow用户

发布于 2016-01-26 15:44:22

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

我确信您的foreach -> foreach -> 构造与$dataRow的结构不够精确地对应;这就是问题所在。

数组:$dataRow is…

  • a array of arrays
  • 从属数组只包含一项 (index=0);这些都是“虚拟”数组,其中…
  • 包含标量条目的真实数组,即相关数据。

所以,它的结构是:

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

您可能遇到了“虚拟”数组的问题。你用你的一次通过(…)以某种方式纠正了这个“虚拟”级别。循环,这实际上是不需要的。

我尝试使用(几乎)您的数据,16行。它还可以处理$dataRow数组中任意数量的行。对于最外层的数组和最内层的数组,有一个foreach循环。其间的虚拟数组由foreach($dataVal…)覆盖构造。

代码

代码语言:javascript
运行
复制
<?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循环将如下所示:

代码语言:javascript
运行
复制
foreach($dataRow as $row => $dataVal) {
    foreach($dataVal as $data) {
        foreach($data as $key => $value) {
            // Process the the items in the data array
        }
    }
}

结果

代码语言:javascript
运行
复制
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    
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34992898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档