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条)

报告相同问题?

悬赏问题

  • ¥100 有偿,谁有移远的EC200S固件和最新的Qflsh工具。
  • ¥15 找一个QT页面+目标识别(行人检测)的开源项目
  • ¥15 有没有整苹果智能分拣线上图像数据
  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析
  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
  • ¥15 LLM accuracy检测
  • ¥15 pycharm添加远程解释器报错