I am trying to store specific data into an array
so I can use it later.
I have tried storing the data within an array by key. But it just constantly loops far more times than it needs to. I set a condition that when the row = "configurable"
(Which should be every 5th or 6th row) it unloads the data and restarts. So, I can put the data into the "Configurable" row and then start again.
I could be doing this completely wrong, but I cannot see another way around this. I have also tried placing the "For"
loop in the Foreach, but it just gave me more problems with the looping.
($Ptype
is a value declared outside of this loop. It should occur every 4 - 6 rows)
$rowArr =
[1] => Array
(
[0] => 5.5
[1] => sku123
[2] => default
[3] => simple
[4] => testData4
[5] => testData5
[6] => testData6
)
[2] => Array
(
[0] => 5.9
[1] => sku456
[2] => default
[3] => simple
[4] => testData4
[5] => testData5
[6] => testData6
)
$rowArr continues for about 1000 rows. I want to get values [1] and [3], and to place them whenever the "if($ptype == 'configurable')" is met. Once this is done, I want to continue within the $rowData array and repeat until the if statement again.
So the output should be (I'll format this a bit):
[5.5, simple, 5.9, simple, ... , ...]
and then it should be removed once the if statement is met, to make room for the new values coming in.
for ($i=1; $i < count($rowArr); $i++) {
$Data[] = $rowArr[$i][1];
$Data[] = $rowArr[$i][3];
// without a "break;" here, it gets too many rows.
}
if($ptype == 'configurable'){
$dataim = implode("," , $Data);
echo $dataim . "
";
$dataim = "";
// If I "die;" here, it fills the first row correctly, but it needs to get every row.
reset($Data);
}
I have also tried (I have tried a lot of moving around breaks etc):
for ($i=1; $i < count($rowArr); $i++) {
$Data[] = $rowArr[$i][1];
$Data[] = $rowArr[$i][3];
if($ptype == 'configurable'){
$dataim = implode("," , $Data);
echo $dataim . "
";
$dataim = "";
reset($Data);
break;
}
}
In Summary:
Store values [1] and [3] from another array
Once the [3] value is configurable, dump the data and start again in the array. continues indefinitely until rows are done.
Actual Result:
Just loops the first 2 values over and over again, not getting any other data if a break is implemented. Without the break in the
foreach
loop, it loops forever.With a
"die;"
in the for loop, it gets the correct data, but only for the first row.