2015-01-26 3 views
-4

검색된 json 배열에서 값을 반환합니다. 나는 일부 데이터가 아닌 다른 사람을 구문 분석 할 수 있습니다, 울부 짖는 소리는 데이터 메신저의보기 내려고입니다 :php cURL undefined index

enter image description here

코드 :

<?php 
    $headers = array(//Set headers data 
    'Content-Type:application/json', 
    'Authorization: Basic '. base64_encode("Username removed:Password removed") //Set basic authentication 
); 
    $today = date('Y/m/d'); 
    $serviceUid='W96397'; 
    $ch = curl_init('https://api.rtt.io/api/v1/json/service/'.$serviceUid.'/'.$today);//serviceUid is set here 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch,CURLOPT_ENCODING, "");   //Set gzip decoding 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  //not sure 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //Set headers in curl options, set previously 
    $result=curl_exec($ch);       //Execute and retrive data 
    curl_close($ch); 
    $result_arr = json_decode($result, true); 

     $serviceUid= $result_arr['serviceUid']; //works fine 
     $runningIdentity = $result_arr['runningIdentity']; //works fine 

     //All commented out bellow do not return any data********* 

     foreach($result_arr['locations'] as $locations){ 
      $crs = $locations['crs']; 
     // $gbttBookedDeparture = $locations['gettBookedDeparture']; 
      $isCall = $locations['isCall']; 
      $isPublicCall = $locations['isPublicCall']; 
     // $realtimeDeparture = $locations['realtimeDeparture']; 
     // $realtimeDepartureActual = $locations['realtimeDepartureActual']; 
     // $platform = $locations['platform']; 
     // $platformConfirmed = $locations['platformConfirmed']; 
     // $platformChanged = $locations['platformChanged']; 
      $displayAs = $locations['displayAs']; 
     } 
    ?>     

오류는 다음과 같습니다 PHP 공지 : 정의되지 않은 인덱스 : 플랫폼 데이터 FL 따르면 pastebin.com/raw.php?i=P5A7s1Ur

+2

왜 오류 행 번호가 무엇인가 말을하지 않습니다? '$ anyArr [ 'platform']'호출은 어디에 있습니까? 그 줄은 이제 코드에 주석 처리됩니다. $ locations [ 'platform'] : ''; ' – Alex

+0

주석 처리 된 모든 행은 일치하지 않으면 오류가 발생합니다. – user3770935

+0

가능한가? 더미의 한 레코드에이 값이 누락 되었습니까? – ethrbunny

답변

0

: 라인 /var/www/html/serviceDetails.php [라인 번호]

편집 : JSON 데이터 일부 요소는 '플랫폼'속성을 포함하지 않습니다 (예제 참조). 속성은 이전에 사용 존재하는 경우가 확인해야합니다

$platform = (isset($locations['platform']))?$locations['platform']:'';

:

그래서 내가 전에 sugessted 기술을 사용합니다.

코드 :

foreach($result_arr['locations'] as $locations){ 

    $platform = (isset($locations['platform']))?$locations['platform']:''; 
    echo '== PLATFORM : '.$platform.' <br />'; 
} 

출력 : == PLATFORM : 7 == PLATFORM : 9 == PLATFORM : 4 == PLATFORM : 2 == PLATFORM : 2 == PLATFORM : 3 == PLATFORM : == PLATFORM : 1 == PLATFORM : == PLATFORM : 2 == PLATFORM : 2 == PLATFORM : == PLATFORM : 1 == PLATFORM : 1 == PLATFORM : == PLATFORM : == PLATFORM : == PLATFORM : 2 == PLATFORM : 3

+0

감사합니다. 내 프로그램을 멈추고있는 많은 빈 배열을 놓쳤다. 고맙습니다. – user3770935

+0

매우 환영합니다 :-) – Alex