Awal Ramdhani
Awal Ramdhani

Reputation: 25

Displays 2 arrays in 1 table

Display 2 arrays in 1 table where 2 columns from 1 array and 1 column from 2nd array

Array 1

Array ( [0] => Object ( [id] => 6 [code] => 303 [aspect] => value [description] =>  ) [1] => Object ( [id] => 7 [code] => 303 [aspect] => value [description] => value  ) [2] => Object ( [id] => 8 [code] => 303 [aspect] => value [description] => value ) [3] => Object ( [id] => 9 [code] => 303 [aspect] => value [description] => value ) [4] => Object ( [id] => 10 [code] => 303 [aspect] => value [description] => value  ) [5] => Object ( [id] => 11 [code] => 303 [aspect] => value [description] => value  ) [6] => stdClass Object ( [id] => 12 [code] => 303 [aspect] => value [description] => value  ) )

and array 2 is

Array ( [0] => 11.9 [1] => 14.29 [2] => 17.46 [3] => 10.32 [4] => 18.65 [5] => 16.27 [6] => 11.11 )

i want to looping key aspect and description for column aspect and description and the other column is the result, where the result column is a loop from array 2

I've tried various loops on the table structure .. like this

<table class="blue-table">
        <thead>
            <tr>
                <th>ASPEK</th>
                <th>URAIAN</th>
                <th>HASIL</th>
            </tr>
        </thead>
        <tbody>
            <?php
            foreach ($array1 as $data) {

            ?>
                <tr>
                    <td><?= $data->aspek ?></td>
                    <td><?= $data->urian ?></td>
                <?php } ?>
                <?php for ($i = 0; $i < count($array2); $i++) { ?>
                    <td><?= $array[$i]; ?></td>
                </tr>
            <?php } ?>
        </tbody>
    </table>

I also have moved the repetition several times ... but it's still not fixed

Upvotes: 0

Views: 56

Answers (1)

Nick
Nick

Reputation: 147156

You need to use the key from your foreach loop on $array1 to access the appropriate value in $array2:

<?php
foreach ($array1 as $key => $data) {
?>
    <tr>
        <td><?= $data->aspek ?></td>
        <td><?= $data->urian ?></td>
        <td><?= $array2[$key]; ?></td>
    </tr>
<?php } ?>

Upvotes: 1

Related Questions