2017-11-06 2 views
0

내 PHP 코트 반환 오류를 얻을 수 없습니다구문 분석 JSON 값

Undefined property: stdClass::$paging in Trying to get property of non-object Use of undefined constant total_items - assumed 'total_items' in

내가 total_items 가치를 얻을 수있는 방법

?

{ 
    "paging": { 
     "total_items": 0, 
     "current_page": 1, 
     "total_pages": 0 
    }, 
    "data": [], 
    "facets": null 
} 
$content = file_get_contents($urll); 
$clean_content = removeBomUtf8($content); 
$decoded = json_decode($clean_content); 

foreach ($decoded->data as $data) 
{ 
    $total_items = (string)$data->paging[0]->total_items; 
    echo total_items; 
} ; 
+2

빈 배열 인 경우 왜'$ decoded-> data'를 통해 반복 하시겠습니까? 그리고 그 json으로부터'total_items' 값을 얻으려면 루프가없는'$ decoded-> paging-> total_items' 일 것입니다. – ArtOsi

답변

1

티르이 :

는 JSON이다.

$data ='{"paging":{"total_items":0,"current_page":1,"total_pages":0},"data":[],"facets":null}'; 

$v = json_decode($data,true); 
echo $v['paging']['current_page']; 
+1

그것은 객체와도 완전히 작동하지만 연관 배열로 변환하는 것도 옵션입니다. – ArtOsi