X
X
XenK2017-09-07 09:28:30
PHP
XenK, 2017-09-07 09:28:30

Comparing arrays and outputting to a table?

There is an array:

Array
(
    [oldAttributes] => Array
        (
            [id] => 1
            [city_id] => 5
            [status] => 0
            [name] => test1
        )

    [newAttributes] => Array
        (
            [id] => 1
            [city_id] => 8
            [status] => 0
            [name] => test2
        )

)

You need to compare two arrays and output an html table, so that it turns out like this:
f43c679939194af9a668d0ac9b6b47ad.PNG
It turned out to be done only like this:
<table style="width: 100%;" border="1">
        <thead>
        <tr>
            <td>Attribute</td>
            <td>Value</td>
        </tr>
        </thead>
        <tbody>
        <?php if (!empty($params)) foreach ($params as $param => $val): ?>
            <?php foreach ($val as $k => $v): ?>
            <tr>
                <td><?= $k ?></td>
                <td><?= $v ?></td>
            </tr>
            <?php endforeach; ?>
        <?php endforeach; ?>
        </tbody>
    </table>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2017-09-07
@XenK

Inside :<tbody>

<?php
foreach ($params['oldAttributes'] as $oldName => $oldValue) {
  $newValue = $params['newAttributes'][$oldName];
  printf( "<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n",
    $oldName,
    $oldValue,
    $newValue === $oldValue ? '' : $newValue
  );
}
?>

It is assumed that the list of attributes in the old and new always completely matches. Otherwise, you need to check.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question