drxpt06820 2019-04-27 10:06
浏览 80
已采纳

无法在foreach循环中存储数据,不断循环

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:

  1. Store values [1] and [3] from another array

  2. Once the [3] value is configurable, dump the data and start again in the array. continues indefinitely until rows are done.

Actual Result:

  1. 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.

  2. With a "die;" in the for loop, it gets the correct data, but only for the first row.

  • 写回答

2条回答 默认 最新

  • douyi6290 2019-04-27 10:40
    关注

    It seems that you are using Magento and want to retrieve simple products of configurable products.

    You have approach this as

    $arr = [
     [5.5,'sku123','default','simple','testData4','testData5','testData6'],
     [5.9,'sku456','default','simple','testData4','testData5','testData6'],
     [5.9,'sku4561','default','configurable','testData4','testData5','testData6'],
     [5.5,'sku123111','default','simple','testData4','testData5','testData6'],
     [5.9,'sku456222','default','simple','testData4','testData5','testData6'],
    [5.9,'sku456123','default','configurable','testData4','testData5','testData6'],
    ];
    $data = [];
    $records = [];
    for($i=0;$i<count($arr);$i++){
     ($arr[$i][3] == 'configurable') ? ($records[$arr[$i][1]] = implode(',',call_user_func_array('array_merge', $data)) AND $data=[]) : ($data[] = [ $arr[$i][1],$arr[$i][3] ]);
    }
    echo '<pre>';
    print_r($records);
    

    Output :

    Array
    (
     [sku4561] => sku123,simple,sku456,simple
     [sku456123] => sku123111,simple,sku456222,simple
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么