0

I begin in PHP. I would like to access on these data from my table with a foreach but I can't.

I tried to follow this post but it does not work (How do I extract data from JSON with PHP?).

My data, stored in a column

[
 {"type":"Travaux dirigés","hour":"15"},
 {"type":"Travaux pratique","hour":"30"}
]

Thank you very much for your help

Community
  • 1
  • 1
Jeremy
  • 1,756
  • 3
  • 21
  • 45

3 Answers3

2

parse json like this, live demo.

$string = '[
     {"type":"Travaux dirigés","hour":"15"},
     {"type":"Travaux pratique","hour":"30"}
    ]';

    $data = json_decode($string, true);

    foreach ($data as $v) {
        echo $v['hour'];
    }
LF00
  • 27,015
  • 29
  • 156
  • 295
0
<?php

$data = '[
     {"type":"Travaux dirigés","hour":"15"},
     {"type":"Travaux pratique","hour":"30"}
    ]';

$getdata = json_decode($data, TRUE);

?>


<ul>
<?php
    foreach($getdata as $value){ 
?>
    <li>
    <p> <?php echo $value['type']; ?>
    <p> <?php echo $value['hour']; ?> 
    </li>
<?php
    }
?>
</ul>
RïshïKêsh Kümar
  • 4,734
  • 1
  • 24
  • 36
-1

Try this:

$json = '[
     {"type":"Travaux dirigés","hour":"15"},
     {"type":"Travaux pratique","hour":"30"}
    ]';

    $data = json_decode($json);

    foreach ($data as $row) {
        print "row=> " . $row->hour;
    }
Aleksander Rezen
  • 877
  • 7
  • 14
  • 1
    Two things to note about this: You probably need to use `$row->hour` instead of `$row['hour']` (because you didn't specify it as an associative array, second parameter to `true`), and there's missing a semicolon on that line. http://php.net/manual/en/function.json-decode.php Not my downvote though! – Qirel May 06 '17 at 15:29