duanlun2827 2018-05-16 04:06
浏览 38
已采纳

如何从PHP中的json获取数据?

I have such json data structure:

$data='[{"day":0,"periods":[{"start":"01:00","end":"02:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"},{"start":"02:30","end":"03:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":1,"periods":[{"start":"01:00","end":"02:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":2,"periods":[{"start":"01:00","end":"01:30","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":3,"periods":[{"start":"02:00","end":"02:30","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":4,"periods":[{"start":"01:00","end":"01:30","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":5,"periods":[{"start":"01:30","end":"02:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"},{"start":"03:00","end":"03:30","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":6,"periods":[]}]';

I want to get all days and start and end time which is in periods.

How to loop threw array and get ? It has 7 days day[0]-day[6] and periods is array which can be empty or have values.

day

periods->start

periods->end

  • 写回答

3条回答 默认 最新

  • dongyi6195 2018-05-16 08:08
    关注

    Like everyone is saying you need to use json_decode() to decode the json in to a php variable. When you add TRUE to that function, you will get an associative array. However, no one is telling you how to loop through this array.

    One way you could do this is by using foreach(). Because you have a multidimensional array you'll need to use foreach() multiple times.

    <?php
    
    $data='[{"day":0,"periods":[{"start":"01:00","end":"02:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"},{"start":"02:30","end":"03:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":1,"periods":[{"start":"01:00","end":"02:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":2,"periods":[{"start":"01:00","end":"01:30","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":3,"periods":[{"start":"02:00","end":"02:30","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":4,"periods":[{"start":"01:00","end":"01:30","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":5,"periods":[{"start":"01:30","end":"02:00","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"},{"start":"03:00","end":"03:30","title":"","backgroundColor":"rgba(254, 0, 0, 0.7)","borderColor":"rgb(42, 60, 255)","textColor":"rgb(0, 0, 0)"}]},{"day":6,"periods":[]}]';
    
    // Decodes a JSON string, When TRUE, returned objects will be converted into associative arrays. 
    $array = json_decode($data, true);
    
    
    /* Output array, uncomment if you would like to see
       echo '<pre>' to make it readable */
    // echo '<pre>';
    // print_r($array);
    
    // foreach lvl in the array we need to use a new foreach() 
    foreach($array as $day) {
        echo 'Day: '. $day['day'] .'<br>';
    
        foreach($day['periods'] as $periodId => $period) {
            echo 'Period '. $periodId .': '. $period['start'] .' - '. $period['end'] .'<br>';
        }
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错