PHP foreach loop through multidimensional array
I have a multidimensional array, how can I use it? I want to use every single array in a loop for
.
What I want to achieve is the ability to put each piece in my database, like db entry no. 0 → 1 and 4 write to db no. 1 → 5 and 6 record in db no. 2 → 1 and 4 and 5 and 6
I have this code:
<?php
print_r($calculatie_id);
for ($i=0; $i<=3; $i++)
{
?>
<tr>
<td>
<?php
foreach ($calculatie_id[$i] as $value)
{
echo $value;
}
?>
print_r($calculatie_id); gives
Array ( [0] => Array ( [0] => 4 [1] => 6 ) [1] => Array ( [0] => 1 [1] => 5 ) [2] => Array ( [0] => 5 [1] => 6 ) [3] => )
But when used foreach
I only get 46
Additional Information:
This array is created by multiple selection in a for loop. Any other suggestions are greatly appreciated.
for ($i=0; $i<=$aantal_regels_corr; $i++)
{
?>
<tr>
<td>
<?php
// maak select name
$calculatie_id = 'calculatie_id'.$i;
// rest van formulier
if($error_vergeten[$i] == "ja"){ $error_omschr = $error_omschr_vergeten[$i]; include('includes/input_error.php'); } ?> <textarea rows="7" cols="80" name="omschrijving[]" /><?php echo $omschrijving[$i]; ?></textarea>
</td>
<td colspan="2">
<select multiple="multiple" width="50" size="7" name="<?php echo $calculatie_id ?>[]" style="width: 150px">
<?php
$sql_calc = "select id, naam, actief from sp_calculatie WHERE (offerte_id = '".$row['off_offerte']."' OR offerte_id = '".$offerte_id."') AND actief = 'ja' ORDER BY id ASC";
$res_calc = mysql_query($sql_calc,$con);
while ($row_calc = mysql_fetch_assoc($res_calc)){
?>
<option value="<?php echo $row_calc["id"] ?>"><?php if (empty($row_calc["naam"])) { echo $row_calc["id"]; } else { echo $row_calc["naam"]; } ?></option>
<?php } ?></select>
</td>
</tr>
<?php } ?>
a source to share
foreach($calculatie_id as $inner_arr)
foreach($inner_arr as $value)
echo $value;
Edit: you should try to find out and understand what's going on here. Then you can do whatever you want with the code I wrote. You said: gives 14561456 I want to use $calculatie_id0=14 $calculatie_id1=56 $calculatie_id2=1456
etc. Then you will need to do something like this:
foreach($calculatie_id as $index => $inner_arr){
echo "$calculatie_id$index = "
foreach($inner_arr as $value)
echo $value;
echo "<br/>";
}
Or you can create the variables you want (which doesn't make sense to me):
foreach($calculatie_id as $index => $inner_arr){
${"calculatie_id$index"} = '';
foreach($inner_arr as $value)
${"calculatie_id$index"} .= $value;
echo ${"calculatie_id$index"}."<br/>";
}
a source to share
I don't understand why your original code snippet is not working. Could it be a fact that $calculatie_id[3]
it is not an array?
I've tried this:
<?php
$calculatie_id = array ( array(4,6), array(1,5), array(5,6), '' );
for ($i=0; $i<=3; $i++)
{
if(!is_array($calculatie_id[$i]))
continue;
echo "$i: ";
foreach ($calculatie_id[$i] as $value)
{
echo $value;
}
echo '<br/>';
}
And what prints:
0: 46
1:15
2: 56
a source to share
Thanks so much for helping me on this, but all the examples I am trying to implement are not working. I have been trying this script for over 2 weeks now. I seem to be wrong.
I posted my code online at www.pws.nl/downloads/sp_offerte_add.rar. This should be easier than what I am writing in code right now.
a source to share