dtxpz8785 2015-08-03 05:57
浏览 26
已采纳

用PHP解析Json输出?

Json Output:

{
    trainno: "12934",
    trainname: "Karnavati Express",
    mon: 1,
    tue: 1,
    wed: 1,
    thu: 1,
    fri: 1,
    sat: 1,
    sun: 1,
    fromname: "Ahmedabad Jn",
    toname: "Mumbai Central"
    }

Need to show output like below table:

enter image description here

problem is how to separate this dynamic value of Run on like MON, Tue...

Thanks

  • 写回答

1条回答 默认 最新

  • dqmchw0071 2015-08-03 06:00
    关注

    After chat this is the final answer.

    $str = file_get_contents('put url here'); 
    $json = json_decode($str, true); 
    
    $day = ""; 
    if($json['mon'] == 1){$day = $day."MON,";} 
    if($json['tue'] == 1){$day = $day."tue,";} 
    if($json['wed'] == 1){$day = $day."wed,";} 
    if($json['thu'] == 1){$day = $day."thu,";} 
    if($json['fri'] == 1){$day = $day."fri,";} 
    if($json['sat'] == 1){$day = $day."sat,";} 
    if($json['sun'] == 1){$day = $day."sun";} 
    
    
    print_r($day) .'<br />';
    

    EDIT 2 Using array

    $str = file_get_contents('put url here'); 
    $json = json_decode($str, true);
    $day2=array();
    if($json['mon'] == 1){array_push($day2 , "MON";} 
    if($json['tue'] == 1){array_push($day2 , "tue";} 
    if($json['wed'] == 1){array_push($day2 , "wed";} 
    if($json['thu'] == 1){array_push($day2 , "thu";} 
    if($json['fri'] == 1){array_push($day2 , "fri";} 
    if($json['sat'] == 1){array_push($day2 , "sat";} 
    if($json['sun'] == 1){array_push($day2 , "sun";} 
    print_r($day2);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭