I want to generate a selectbox
using two arrays, one containing the country codes and another containing the country names.
This is an example:
<?php
$codes = array('tn','us','fr');
$names = array('Tunisia','United States','France');
foreach( $codes as $code and $names as $name ) {
echo '<option value="' . $code . '">' . $name . '</option>';
}
?>
This method didn't work for me. Any suggestions?
Use
array_combine()
to fuse the arrays together and iterate over the result.Why not just consolidate into a
multi-dimensionalassociative array? Seems like you are going about this wrong:becomes:
If it would be possible to do as you like, the foreach for 2 arrays would be programmed like that:
My solution isn't perfect but is okay for most purposes.
array_combine()
worked great for me while combining$_POST
multiple values from multiple form inputs in an attempt to update products quantities in a shopping cart.You should try this for the putting 2 array in singlr foreach loop Suppose i have 2 Array 1.$item_nm 2.$item_qty
Your code like this is incorrect as foreach only for single array:
Alternative, Change to this: