2017-02-27 77 views
-1

我想將json數組打印到它的值中我在json解碼後有下面的數組,我需要在循環中打印它,因爲它有很多項目。 例如,我必須打印值名字,姓氏,我將如何打印它。如何通過json響應打印數組中的值?

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) 

示例代碼我做:

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

但它不打印任何價值。

###從下面的答案全部工作代碼:###

助手文件:

####### --- 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]。你也應該看到,[content]是一個數組,所以你要做的

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

或使用第一個內第二foreach循環。

+0

謝謝@Wolen我打印如下: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; 
} 

或只是做

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轉換一個有效的JSON到stdClass的,對象實現這一點。

嘗試這種情況:

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

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

謝謝瓦拉爾。我已經完成了json解碼。謝謝你的答案。 – gitu