-2

I want to parse this json in php and show it in a table.

{"Files":[{"name":"Tester","Dir":true,"path":"\/stor\/ok"},{"name":"self","Dir":true,"path":"\/stor\/nok"}]}
Eddie
  • 26,593
  • 6
  • 36
  • 58
YICT IR
  • 81
  • 1
  • 2
  • 7

2 Answers2

3

You can use simple foreach loop like this.

Code

<?php

$json = '{"Files":[{"name":"Tester","Dir":true,"path":"/stor/ok"},{"name":"self","Dir":true,"path":"/stor/nok"}]}';
$json = json_decode($json, true);

?>
<!DOCTYPE html>
<html>
<body>
    <table border="1">
        <tr><td>name</td><td>Dir</td><td>path</td></tr>
        <?php foreach ($json["Files"] as $k => $v): ?>
            <tr>
                <td><?php echo htmlspecialchars($v["name"]); ?></td>
                <td><?php echo htmlspecialchars($v["Dir"]); ?></td>
                <td><?php echo htmlspecialchars($v["path"]); ?></td>
            </tr>
        <?php endforeach; ?>
    </table>
</body>
</html>

Result

screenshot result

Ammar Faizi
  • 1,393
  • 2
  • 11
  • 26
1

I created you some little code example. You decode your string to json array. Thereafter you can parse the Files array with a foreach loop. Then in the foreach you can output/ save your values. In this case I output the name.

$string = '{"Files":[{"name":"Tester","Dir":true,"path":"\/stor\/ok"},{"name":"self","Dir":true,"path":"\/stor\/nok"}]}';

$string = json_decode($string, true);

if ($string != null)
{
    foreach ($string['Files'] as $values)
    {
        echo $values['name'];
        echo "\n";
    }
}

Output:

Tester
self

davidev
  • 7,694
  • 5
  • 21
  • 56