2015-07-03 71 views
1

我想为我的项目使用这个铁路API。如果我输入一个正确的数字,那么它可以很好地工作,但如果我输入一个随机的10位数字,那么我会收到第20行和第21行中未定义的偏移量:0错误。我对此很陌生,而且我已经搜索了很多解决方案,找不到任何东西。未定义的偏移量0在PHP中的json值

<?php 
$json = ''; 
if(isset($_POST['pnrQ'])){ 
    $searchParam = $_POST['pnrQ']; 
$replacedStr = str_replace(" ","",$searchParam); 
$url = 'http://api.railwayapi.com/pnr_status/pnr/'.$replacedStr.'/apikey//'; 
$content = file_get_contents($url); 
$json = json_decode($content, true); 
}else{ 
    echo "something went wrong, please notify to admin [[email protected]]"; 
} 
$dateOfJourney = $json['doj']; 
$pnr = $json['pnr']; 
$fromStation = $json['from_station']['name']; 
$fromStationCd = $json['from_station']['code']; 
$toStation = $json['to_station']['name']; 
$toStationCd = $json['to_station']['code']; 
$trainNum = $json['train_num']; 
$trainName = $json['train_name']; 
$currStatus = $json['passengers'][0]['current_status']; //line 20 
$bookingStatus = $json['passengers'][0]['booking_status']; //line 21 
$isChartPrep = $json['chart_prepared']; 
$isChartPrepd = ($isChartPrep=='N') ? 'NO' : 'YES'; 
$cls = $json['class']; 
$totPass = $json['total_passengers']; 
?> 
+0

其中没有。你插入和谈论? – Vinay

+0

打印json变量并检查值 –

+0

发布您的$ json数据 – rocky

回答

0

使用var_exportprint_r功能检查究竟在$ JSON [“乘客”]

可能是JSON值具有不同的结构。

编辑:

可能是由于输入错误,你得到了JSON的答复具有不同的结构。所以你可以先检查出来,然后使用if条件来查看特定的数组元素是否存在。例如: : - isset($json['passengers'])!empty($json['passengers'])

+0

非常感谢。它的工作现在完美:D –