用PHP解析Json输出?

时间:2015-08-03 05:57:13

标签: php json

Json输出:

{
    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"
    }

需要显示如下表所示的输出:

enter image description here

问题是如何在MON,Tue ...

之间分离Run的动态值

谢谢

1 个答案:

答案 0 :(得分:1)

聊天后,这是最终的答案。

$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 />';

编辑2使用数组

$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);