如何使用PHP中的foreach访问这些数据

时间:2017-05-06 15:22:22

标签: php json string

我从PHP开始。我想从foreach访问这些数据,但我不能。

我试图关注这篇文章,但它不起作用(How do I extract data from JSON with PHP?)。

我的数据,存储在一列

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

非常感谢您的帮助

3 个答案:

答案 0 :(得分:2)

像这样解析json,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'];
    }

答案 1 :(得分: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>

答案 2 :(得分:-1)

试试这个:

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

    $data = json_decode($json);

    foreach ($data as $row) {
        print "row=> " . $row->hour;
    }