duanlaofu4108 2018-12-03 18:53
浏览 33

PHP检查循环之前的值

I have a JSON multi-dimensional array of products from Shopify that I am looping through. Each product has 3 options and each option can have infinite numbers of values.

For example:

  • Lamp (product)
    • Glass Finish (option)
      • Clear (value)
      • Smoke (value)
    • Metal Finish (option)
      • Polished Chrome (value)
    • Drop (option)
      • 600mm (value)
      • 800mm (value)

Within the loop I am creating rows within a 'repeater' field in a CMS (one for each option and then a repeater within each option for each value).

This is all fine BUT I want to do a few checks as the array of products is cached to every 30 minutes so if the cache was updated and either an option was removed or a value to that option was added then it should update.

I have my first check in place (loop through all the existing repeater options that have been added, before doing anything, and if an option already exists then skip it (and thus not creating multiple of the same options).

What I need to do is to check each of the values to see if any new ones have been added. I can write the code to actually add the value to the repeater field but I am unsure how to check as by this point, if the option already exists, it skips over.

Any thoughts?

foreach($product['options'] as $option) {

    foreach($p->shop_product_options as $options) {

         // If this option already exists... then skip the parent loop on this product
        if ($options->global_text == $option['name']) {
            continue 2;
        }

    }

    $options = $p->shop_product_options->getNew();
    $options->of(false);
    $options->global_text = $option['name'];
    $options->save();
    $p->shop_product_options->add($options);
    foreach($option['values'] as $o) {
        $values = $options->shop_product_options_option->getNew();
        $values->of(false);
        $values->global_text = $o;
        $values->save();
        $options->save();
        $options->shop_product_options_option->add($values);
    }
    $options->save();
}
  • 写回答

1条回答 默认 最新

  • douhuzhi0907 2018-12-03 20:32
    关注

    Still not sure I am completely understanding what you are after but it sounds like you need something like this :

    foreach($product['options'] as $option) {
    
        $optionNames = array_column($options, 'name');
        $options = $p->shop_product_options->getNew();
    
        foreach($option['values'] as $o) {
    
            $values = $options->shop_product_options_option->getNew();
    
            if($values->global_text === $o && in_array($option->global_text, $optionNames, true)) {
                continue 2;
            }
            $values->of(false);
            $values->global_text = $o;
            $values->save();
            $options->shop_product_options_option->add($values);
        }
    
        $options->of(false);
        $options->global_text = $option['name'];
        $p->shop_product_options->add($options);
        $options->save();
    }
    

    array_column will create an array getting all the values of a multidimensional array when the key is name, and then the if statement will look for a combination of name and value.

    And since the saving logic for the option is below the loop for the values, it will check the cache before saving anything, this might not be a copy/paste answer, i'm just giving you some logic to work with

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号