Jasper Renema
Jasper Renema

Reputation: 143

Iterating through multidimensional array in php

I have this $photo array that I receive from a form post:

Array (
    [274] => Array (
        [10x10] => 0
        [10x15] => 3
        [13x13] => 2
        [13x18] => 0
        [15x15] => 0
        ...
    )
    [273] => Array (
        [10x10] => 1
        [10x15] => 0
        [13x13] => 0
        [13x18] => 0
        [15x15] => 0
        [20x20] => 0
        [20x28] => 2
        [30x30] => 0
        [30x40] => 0
        ...
    )
)

I can echo this array, except the fields with value 0.

<?php
echo "<table>";

foreach($foto as $key=>$value){

    foreach($value as $k => $v){

        if ($v != "0") {
            echo'<tr>';
            echo '<td>'  . $k . '</td>';
            echo '<td>'  . $v . '</td>';
            echo '</tr>';
        }
    }
}
echo "</table>";
?>

Which will result in:

10x15   3
13x13   2
10x10   1
20x28   2

But I also need the id of the array, in this case 274. I'm staring at this code for hours, been browsing the internet but I can't see it. How do i extract the id of the array (274).

Help/tips appreciated.

Upvotes: 0

Views: 702

Answers (1)

Audite Marlow
Audite Marlow

Reputation: 1054

Try this:

<?php
echo '<table>';

foreach ($foto as $key => $value){
    foreach ($value as $k => $v){
        if ($v != "0") {
            echo '<tr>';
            echo '<td>' . $key . '</td>';
            echo '<td>' . $k . '</td>';
            echo '<td>' . $v . '</td>';
            echo '</tr>';
        }
    }
}

echo '</table>';
?>

Upvotes: 2

Related Questions