sonia maklouf sonia maklouf - 5 months ago 11
PHP Question

two forech loop on if statement

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.

Answer

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