views:

503

answers:

7

I have an multidimensional array, how can I use it? I want to use each separate array in a for loop.

What I want to achive is to be able to put each part in my database like entry in db no. 0 -> 1 and 4 entry in db no. 1 -> 5 and 6 entry 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 using the foreach I only get 46

Some extra information:

This array is created by an multiple select in an for loop. Any other suggestions are welcome.

    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'); } ?>&nbsp;<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: 

You need to nest your foreach loops (so you have a loop inside a loop)

webdestroya
+3  A: 
foreach($calculatie_id as $inner_arr)
    foreach($inner_arr as $value)
        echo $value;

Edit: you should try to learn 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 have 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 those variables you want (which does not make any sense for 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/>";
}
Cristian
Array ( [0] => Array ( [0] => 1 [1] => 4 ) [1] => Array ( [0] => 5 [1] => 6 ) [2] => Array ( [0] => 1 [1] => 4 [2] => 5 [3] => 6 ) [3] => ) gives14561456I want to use$calculatie_id0=14$calculatie_id1=56$calculatie_id2=1456etc.
Muiter
A: 

You could try:

foreach ($calculatie_id as $key => $value) {
  echo "$calculatie_id:".$key;
  for ($i=0;$i<=count($value);$i++) {
    echo $value[$i];
  }
}
thesunneversets
thx but this is the result:Array:014Array:156Array:21456Array:3
Muiter
A: 

first of all, next time use this form:

print '<pre>';
print_r($array);
print '</pre>';

that give you a more clean view or array structure like this:

//$array
Array ( //$key_1
        [0] => Array ( //$val_1
                $key_2
                [0] => 4 //$val_2
                [1] => 6 //$val_2

        //$key_1        ) 
        [1] => Array ( 

                [0] => 1 //$val_2
                [1] => 5 //$val_2

        //$key_1        ) 
        [2] => Array ( 

                [0] => 5 //$val_2
                [1] => 6 //$val_2

        //$key_1        ) 
        [3] =>           //this key have no $val_2

      )

DIFFERENT WAYS OF WORKING WITH ARRAY:


foreach ( $array as $key_1 => $val_1 ) {
foreach ( $val_1 as $key_2 => $val_2 ) {
echo $val_2; // 4 , 6 , 1 , 5 , 5 , 6
  }
}

echo $array[0][0]; // 4
echo $array[0][1]; // 6

echo $array[1][0]; // 1
echo $array[1][1]; // 5

echo $array[2][0]; // 5
echo $array[2][1]; // 6

foreach ( $array as $key => $val ) {
echo $val[0]; // 4 , 1 , 5
echo $val[1]; // 6 , 5 , 6
}
aSeptik
thx for this tip.
Muiter
you welcome Muiter! see also the update!
aSeptik
I have this result:1 4 5 6 1 4 5 6I'm confused. What I want to achive is to be able to put each part in my database likeentry in db no. 0 -> 1 and 4entry in db no. 1 -> 5 and 6entry in db no. 2 -> 1 and 4 and 5 and 6
Muiter
i think all it's little fuzzy, have this array some kind of identifier for each sub_array!? what i mean is, there is a way for distinguish each group [0], [1]. [2], [3] from another!?
aSeptik
A: 

Basically what you have is an array like this:

array(
 array(
  data
 )
)

All you need is:

foreach($yourArray as $innerArray)
 foreach($innerArray as $value)
  echo $value;

That will output what you want. I'm not particularly sure why you're doing a for() loop then a foreach.

Daniel
I have added more info to the opening post.
Muiter
A: 

I don't understand why your original code snippet isn't working. Could it be the fact that $calculatie_id[3] is not an array?

I 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 that prints:

0: 46
1: 15
2: 56

Kip
How to prevent tat is uses an array that doesn't exists?
Muiter
@Muiter: well like i said, you can use `if(!is_array($calculatie_id[$i])) continue;` to go on to the next iteration of the loop
Kip
A: 

Many thanks for helping me out this far but all the examples I try to implement are not working. I have been trying this script for over 2 weeks now. It seems I'm going all the wrong way.

I have put my code online at www.pws.nl/downloads/sp_offerte_add.rar. It must be easier then what I'm writing in the code right now.

Muiter