douhuibo5635 2016-06-22 14:10
浏览 122
已采纳

在if语句中有两个foreach循环

Wondering if it's possible to have a foreach loop call on if statement like this :

if (class_exists('WooCommerce')) { 
    foreach (array_combine($val1, $val2) as $key => $row_values)
}
else {
    foreach ( $val1 as $key => $row_values ) 
}

{ begin of loop code

What I'm trying to do is to have a foreach loop if woocommerce plugin is active and to have another foreach loop if woocommerce isn't active.

  • 写回答

2条回答 默认 最新

  • dshxbrzgi090608692 2016-06-22 14:16
    关注

    This is not possible the way you try it, since that is invalid syntax.

    What you can do is something like that:

    <?php
    $iterationArray = class_exists('WooCommerce') ? array_combine($val1, $val2) : $val1;
    foreach ($iterationArray as $key => $row_values) {
        // begin of loop code
    } // end loop
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?