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,554
  • 1
  • 18
  • 35

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
  • 24,667
  • 25
  • 136
  • 263
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,356
  • 1
  • 23
  • 33
-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;
    }
argoden
  • 827
  • 6
  • 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