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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵