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 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题