PHP循环从excel的数组 [英] PHP Looping array from excel
问题描述
我试图得到我从excel使用foreach和for循环上传的数据,但奇怪的是,当我上传excel文件w / 12行它成功插入,但是当我上传excel w / 13行和它的唯一的获取最后一行,我得到的错误未初始化的偏移:8
。
这是数组(12)
{
[0])当我上传excel / 12行时, => 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]); $ fArray [2],$ fArray [3],$ fArray [4],$ fArray [5]。
$ this-> ,$ fArray [6],$ fArray [7],$ fArray [8],$ flag2);
}
}
}
echo'< / pre>';
}
$ data ['mode'] ='空';
$ data ['message'] ='数据成功上传!';
$ this-> session-> unset_userdata($ dataRow);
$ this-> session-> unset_userdata('Count');
$ this-> load-> view('home_view',$ data);
我的问题是为什么我只得到最后一行,当我开始插入13行,
首先,我创建了一个 $ dataRow 数组类似于你的 var_dump 显示,并尝试它与你的代码:与你说的相同的结果。然后我看看 $ dataRow 和你的循环的结构。
我相信你的 foreach - foreach - > 构造不够精确对应于 $ dataRow 的结构;有问题。
数组: $ dataRow 是…
- 数组。
- 下层数组只包含一个项(index = 0);这些是虚拟数组,其…
- 包含实际的标量条目数组,即相关数据。
- $ dataRow
- (Dummy)数组只包含一个元素。
- 数据
因此,结构是:
你很可能用dummy数组。并且你用你的一遍(…)循环以某种方式纠正了这个虚拟级别,实际上不需要它。
我试过它(几乎)你的数据,16行。它也适用于 $ dataRow 数组中的任意数量的行。对于最外部的数组和最内部的数组有一个 foreach 循环。
代码
<?php
$ dataRow = 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 (CC-00057,6,grapes,2,box,5,A1012,1101,42111,asd)),
array ,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)) (数组(CC-00057,12,grapes,2,box,5,A1012,1101,42111,asd)) ,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>结果< / h2>< pre>
//最外层数组
foreach($ dataRow as $ row => $ dataVal){
//数据数组在$ dataVal [0]中,最内层数组
// - >显示数据数组
$ res ='';
foreach($ dataVal [0] as $ key => $ value){
$ res。=((empty($ res))?'':',')。 $ key = $ value;
}
echoRow $ row:。 $ res。 PHP_EOL;
}
echo'< / pre>';
}
?>
显然,如果dummy数组可以有多个条目, 循环如下:
foreach($ dataRow as $ row => $ dataVal){
foreach($ dataVal as $ data){
foreach($ data as $ key => $ value){
//处理数据数组中的项
}
}
}
结果
行0:0 = CC-00057,1 = 1,2 =苹果,3 = 45,4 = pc,5 = A1011,6 = 1101,7 = 42110,8 = asd
行1:0 = CC-00057,1 = 2,2,葡萄,3 = 2,4 =箱,5 = 5,6 = A1012,7 = 1101,8 = 42111,9 =
行2:0 = CC-00057,1 = 3,2 =葡萄,3 = 2,4 =盒,5 = 5,6 = A1012,7 = 1101,8 = 42111,9 = asd
第3行:0 = CC-00057,1 = 4,2 =葡萄,3 = 2,4是盒子,5 = 5,6 = A1012,7 = 1101,8 = 42111,9 = asd
行4:0 = CC-00057,1 = 5,2 =葡萄,3 = 2,4 =盒,5 = 5,6 = A1012,7 = 1101,8 = 42111,9 = asd
第5行:0 = CC-00057,1 = 6,2 =葡萄,3 = 2,4 =盒,5 = 5,6 = A1012,7 = 1101,8 = 42111,9 = asd
行6:0 = CC-00057,1 = 7,2 =葡萄,3 = 2,4 =盒,5 = 5,6 = A1012,7 = 1101,8 = 42111,9 = asd
行7:0 = CC -00057,1 = 8,2 =葡萄,3 = 2,4 =盒,5 = 5,6 = A1012,7 = 1101,8 = 42111,9 = asd
第8行:0 = CC-00057 ,1 = 9,2 =葡萄,3 = 2,4 =盒,5 = 5,6 = A1012,7 = 1101,8 = 42111,9 = asd
行9:0 = CC-00057,1 = 10,2 =葡萄,3 = 2,4 =盒,5 = 5,6 = A1012,7 = 1101,8 = 42111,9 = asd
行10:0 = CC-00057,1 = 11 ,2 =葡萄,3 = 2,4 =盒,5 = 5,6 = A1012,7 = 1101,8 = 42111,9 = asd
第11行:0 = CC-00057,1 = 12,2 =葡萄,3 = 2,4 =盒,5 = 5,6 = A1012,7 = 1101,8 = 42111,9 = asd
行12:0 = CC-00057,1 = 13,2 =葡萄,3 = 2,4 =盒,5 = 5,6 = A1012,7 = 1101,8 = 42111,9 = asd
行13:0 = CC-00057,1 = 14,2 =葡萄,3 = 2,4 =盒,5 = 5,6 = A1012,7 = 1101,8 = 42111,9 = asd
行14:0 = CC-00057,1 = 15,2 =葡萄,3 = 2 ,4 =方框,5 = 5,6 = A1012,7 = 1101,8 = 42111,9 = asd
行15:0 = CC-00057,1 = 16,2 =葡萄,3 = = box,5 = 5,6 = A1012,7 = 1101,8 = 42111,9 = asd
行16:0 = CC-00057,1 = 17,2 =葡萄,3 = 2,4 = ,5 = 5,6 = A1012,7 = 1101,8 = 42111,9 = asd
I'm trying to get the data that I uploaded from excel using foreach and for loop, but the weird thing is when I upload excel file w/ 12 rows it successfully insert, but when I upload excel w/ 13 rows and up its only get the last row and I'm getting the error Uninitialized Offset :8
.
this is the array when I uploaded excel w/ 12 rows.
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" } } }
this is when 13 rows it only get the last row.
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" } }
below is my code.
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);
My question is why I'm only get the last row when I'm start inserting 13 rows and up, is this on my foreach or for loop syntax?
First of all, I created a $dataRow array similar to what your var_dump shows and tried it with your code: the same result as you say. Then I looked at the structure of $dataRow and your loops.
I'm sure your foreach -> foreach -> for construct does not accurately enough correspond to the structure of the $dataRow; there's the problem.
The array: $dataRow is…
- an array of arrays.
- The subordinate arrays contain exactly one item (index=0); these are "dummy" arrays, which…
- contain a real array of scalar entries, i.e. the relevant data.
So, the structure is:
- $dataRow
- (Dummy) array, contains just one element
- array of data
- data item
You struggled probably with the "dummy" array. And you corrected this "dummy" level somehow with your one-pass for(…) loop, which in fact is not needed.
I tried it with (nearly) your data, 16 rows. It also works with any number of rows in the $dataRow array. There is a foreach loop for the outer-most array and the inner-most array. The dummy array in between is covered by the foreach($dataVal[0]…) construct.
Code
<?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>';
}
?>
Obviously, if the "dummy" array could have more than one entry, the nested foreach loops would look like this:
foreach($dataRow as $row => $dataVal) {
foreach($dataVal as $data) {
foreach($data as $key => $value) {
// Process the the items in the data array
}
}
}
Result
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
这篇关于PHP循环从excel的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!