2017-02-27 1 views
-1

json 배열을 해당 값으로 인쇄하려고합니다. json 디코딩 후 다음 배열이 있고 많은 항목이 있으므로 루프에서 인쇄해야합니다. 예를 들어, firstName, 성을 인쇄해야합니다. 어떻게 인쇄합니까?배열에서 값을 인쇄하는 방법 json 응답으로 반환 하시겠습니까?

$arrayl = gettviewers($post->id,10); 
foreach($arrayl as $valuel) { 
    print $value1->content->firstName; 
} 

을하지만 모든 값을 인쇄되지 않습니다

stdClass Object ([content] => Array ([0] => stdClass Object ([id] => 5 [firstName] => Ali [lastName] => S [profilePhotoUrl] => https://prn-spe-images.s3-ap-southeast-1.amazonaws.com/user-profiles/5.jpg [handle] => aliya) [1] => stdClass Object ([id] => 69 [handle] => hhtc)) [last] => 1 [totalPages] => 1 [totalElements] => 2 [numberOfElements] => 2 [first] => 1 [sort] => [size] => 10 [number] => 0) 

샘플 코드는 내가 했어요.

### 아래 답변에서 전체 작업 코드 : ###

도우미 파일 : 뷰에서

####### --- Get live viwers list for each broadcast --- ######## 
if (! function_exists('gettviewers')) 
{ 
     function gettviewers($broId,$size){ 
     $CI =& get_instance(); 
     $url = API_SERVER.'broadcasts/public/'.$broId.'/top-viewers?size='.$size; 
     $json = json_decode(file_get_contents($url), true); 
     return $json; 
     } 
} 

:

$arrayl = gettviewers($post->id, WI_LIVEUSERSIZE); 
       foreach($arrayl as $value1) 
       { 
       print $value1[0][firstName]; 
       } 

답변

0

의 Foreach은 물체 또는 어레이의 모든 요소를 ​​'얻어'. 당신이 foreach는 할 그래서 때 $value1

stdClass Object ([content] => Array ([0] => stdClass Object ([id] => 5 [firstName] => Ali [lastName] => S [profilePhotoUrl] => https://prn-spe-images.s3-ap-southeast-1.amazonaws.com/user-profiles/5.jpg [handle] => aliya) [1] => stdClass Object ([id] => 69 [handle] => hhtc)) [last] => 1 [totalPages] => 1 [totalElements] => 2 [numberOfElements] => 2 [first] => 1 [sort] => [size] => 10 [number] => 0) 

아니라

[content] => Array ([0] => stdClass Object ([id] => 5 [firstName] => Ali [lastName] => S [profilePhotoUrl] => https://prn-spe-images.s3-ap-southeast-1.amazonaws.com/user-profiles/5.jpg [handle] => aliya) [1] => stdClass Object ([id] => 69 [handle] => hhtc)) [last] => 1 [totalPages] => 1 [totalElements] => 2 [numberOfElements] => 2 [first] => 1 [sort] => [size] => 10 [number] => 0 

그래서 당신의 $value1 이미 [content]입니다. 또한 당신은 당신이

$arrayl = gettviewers($post->id,10); 
foreach($arrayl as $valuel) { 
    print $value1[0]->firstName; 
} 

을하거나 첫 번째 안에 두 번째 foreach 루프를 사용 할 수 있도록 [content]이 배열 인 것을 알 수 있습니다.

+0

감사합니다 @ 월 렌 나는 이렇게 인쇄 : print $ value1 [0] [firstName]; – gitu

-1

을 수행합니다 json_encoded 배열을 인쇄하려면 이

$array=json_decode($data,true); //data is your json_encoded data 

foreach($array as $key=>$value) 
{ 
echo $key . ' ' . $value . PHP_EOL; 
} 

또는 그냥 do

print_r($array); 
해시 한 후

+0

도움 주셔서 감사합니다. Dimi – gitu

0

$arrayl = gettviewers($post->id,10); 


    if (is_object($arrayl)) { 
     // Gets the properties of the given object 
     // with get_object_vars function 
     $arrayl = get_object_vars($arrayl); 
    } 

    if (is_array($arrayl)) { 
     /* 
     * Return array converted to object 
     * Using __FUNCTION__ (Magic constant) 
     * for recursive call 
     */ 
     $arrayl = array_map(__FUNCTION__, $arrayl); 
    } 
    else { 
     // Return array 
     return $arrayl; 
    } 

var_dump($arrayl) or print_r($arrayl); 

또는

foreach($arrayl as $key=>$val){ 
    echo $val->content[$key] 
    } 
+0

왜 이것을 true로 설정하고 있습니까? 그는 배열이 아닌 stdObject를 갖기를 원합니다. –

0

당신은 json_decode

json_decode는 stdClass 객체에 대한 유효한 JSON 변환하여이를 달성 할 수보십시오.

이 시도 :

$arrayl = json_decode(gettviewers($post->id, 10)); 

foreach($arrayl as $value1) 
{ 
    print $value1->content->firstName; 
} 
+0

Vural에 감사드립니다. 나는 이미 json 디코딩을했다. 답변 감사합니다. – gitu

관련 문제