我试图使用foreach和for循环从excel中获取上传的数据,但奇怪的是,当我上传12行的excel文件时,它成功插入了,但是当我上传的是w/ 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循环语法上吗?
发布于 2016-01-26 15:44:22
首先,我创建了一个类似于var_dump所显示的$dataRow数组,并用您的代码进行了尝试:结果与您所说的相同。然后我研究了$dataRow的结构和你的循环。
我确信您的foreach -> foreach -> 构造与$dataRow的结构不够精确地对应;这就是问题所在。
数组:$dataRow is…
所以,它的结构是:
。
您可能遇到了“虚拟”数组的问题。你用你的一次通过(…)以某种方式纠正了这个“虚拟”级别。循环,这实际上是不需要的。
我尝试使用(几乎)您的数据,16行。它还可以处理$dataRow数组中任意数量的行。对于最外层的数组和最内层的数组,有一个foreach循环。其间的虚拟数组由foreach($dataVal…)覆盖构造。
代码
<?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
https://stackoverflow.com/questions/34992898
复制相似问题