menkes
menkes

Reputation: 338

Loop deep subarray elements at known key path in a multidimensional array

I have a SOAP result set that the nuSoap extension has turned into a nice array for me. I'm sure I could scratch out some long way of looping it for what I want - but it seems there must be a faster way to just loop through the specific data elements. Here is the array:

Array
(
    [xxxResult] => Array
    (
        [NewDataSet] => Array
        (
            [Table] => Array
            (
                [0] => Array
                (
                    [ID] => 472
                    [Name] => abc
                    [Weight] => 0.15
                    [AppID] => 5133356895445
                )

                [1] => Array
                (
                    [ID] => 7396
                    [Name] => def
                    [Weight] => 0.11
                    [AppID] => 51348575554
                )

            )

        )

    )

)

So what I want to do is simply loop through this such that I get:

<tr>
    <td>[ID]</td>
    <td>[Name]</td>
    <td>[Weight]</td>
    <td>[AppID]</td>
</tr>

...for each table row.

Seems there should be a quicker way than [xxxResult][NewDataSet][Table][0][ID] etc.

Upvotes: 0

Views: 1038

Answers (2)

Jonas B
Jonas B

Reputation: 2391

Like:

 for ($row = 0; $row < 2; $row++){
echo "<tr>";

for ($col = 0; $col < 4; $col++)
{
    echo "<td>".$myarray[$row][$col]."</td>";
}

echo "</tr>";

}

Of course if the amount of rows or cols changes you need to get the length of the arrays.

Upvotes: 0

Ionuț G. Stan
Ionuț G. Stan

Reputation: 179098

Like this?

<?php

$tables = $soapResult['xxxResult']['NewDataSet']['Table'];

foreach ($tables as $table) {

?>
    <tr>
        <td><?php echo $table['ID']; ?></td>
        <td><?php echo $table['Name']; ?></td>
        <td><?php echo $table['Weight']; ?></td>
        <td><?php echo $table['AppID']; ?></td>
    </tr>
<?php

}

Upvotes: 4

Related Questions