如何使用Json数据循环数组

时间:2017-07-14 06:56:54

标签: php json wordpress

我有一个示例数组get form database列,如下所示:

CID     T1                    Name    T2                     delta
101    1900-01-01 12:31:58.193  Tom    1900-01-01 12:31:57.193   60s
102    1900-01-01 12:31:57.193  John   1900-01-01 12:31:57.193   0s 
103    1900-01-01 12:44:03.098  Mary   1900-01-01 12:34:31.956  ~600s 
104    1900-01-01 12:44:03.111  Rocky  1900-01-01 12:31:57.172  ~700s

我如何解码这个json并在foreach php中循环它?提前谢谢。

4 个答案:

答案 0 :(得分:2)

尝试以下代码

$array=array('{"data_1":"content_1","data_2":"content_2"}','{"data_1":"content_1","data_2":"content_2"}');
foreach($array as $a)
{
   $data=json_decode( $a );
    //print_r($data);
    foreach($data as $k=>$d)
    {
        echo $k.':'.$d;
        echo "<br>";
    }
}
  

输出

data_1:content_1
data_2:content_2
data_1:content_1
data_2:content_2

答案 1 :(得分:0)

你可能必须这样开始:

$JSONGetter = json_decode($GETjson, true);
foreach ($JSONGetter as $value){
$DataContent = $value['data_1'];

答案 2 :(得分:0)

var_dump(array_map(function ($value) {
    return json_decode($value, true);
}, [
    0 => '{"data_1":"content_1","data_2":"content_2"}',
    1 => '{"data_1":"content_1","data_2":"content_2"}'
]));

答案 3 :(得分:0)

尝试以下代码

<?php 
     $nomor = 1; 
     foreach ($data_array as $data) : 
        $data_student_array = json_decode($data->DATA_STUDENT, true); ?> 
        <?php
         $nomor1 = 1; 
        foreach($data_student_array as $data_student_value)
        {
            ?>
             <tr> 
                <td><?php echo $nomor1; ?></td> <td><?php echo $data_student_value['data_1']; ?></td> 
                <td><?php echo $data_student_value['data_2']; ?></td> 
           </tr> 
            <?php
            $nomor1++; 
        }
        ?>

       <?php $nomor++; 
    endforeach; 
    ?>