Created
June 10, 2015 12:59
-
-
Save isotrope/49fde0fa635a95f21943 to your computer and use it in GitHub Desktop.
Two ways to iterate through arrays in PHP for Jon Mather
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Getting the array | |
$product_group = rwmb_meta( 'products' ); | |
/** | |
* Keys = | |
* $product_group = rwmb_meta( 'products'); | |
* 'sw_swsku'; | |
* 'sw_swdescription'; | |
* 'sw_swprice'; | |
* 'sw_swqty'; | |
*/ | |
/** | |
* Notice that the array has indexes like 0, 1,... | |
* | |
* For an ordered array, you can simply loop based on count... | |
* http://php.net/manual/en/control-structures.for.php | |
*/ | |
for ( $i = 0; $i <= count( $product_group ); $i ++ ) { ?> | |
<div class="field-sku"><?php echo $product_group[ $i ]['sw_swsku']; ?></div> | |
<div class="field-description"><?php echo $product_group[ $i ]['sw_swdescription']; ?></div> | |
<div class="field-price"><?php echo (float) $product_group[ $i ]['sw_swprice']; ?></div> | |
<div class="field-qty"><?php echo (int) $product_group[ $i ]['sw_swqty']; ?></div> | |
<?php } | |
/** | |
* If you want to stick to the foreach, keep in mind that in an ordered array, you'll just want to do... | |
* | |
* http://php.net/manual/en/control-structures.foreach.php | |
*/ | |
foreach ( $product_group as $product ) { ?> | |
<div class="field-sku"><?php echo $product['sw_swsku']; ?></div> | |
<div class="field-description"><?php echo $product['sw_swdescription']; ?></div> | |
<div class="field-price"><?php echo (float) $product['sw_swprice']; ?></div> | |
<div class="field-qty"><?php echo (int) $product['sw_swqty']; ?></div> | |
<?php } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment