问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/34992898/php-looping-array-from-excel