我正在尝试将json数组打印到其值中我在json解码后有以下数组,我需要在循环中打印它,因为它有很多项。 例如,我必须打印值 firstName,lastName ,我将如何打印它。
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 :(得分: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]
}
答案 1 :(得分:0)
您可以使用json_decode
实现此目的
json_decode
将有效的JSON转换为stdClass-Object。
试试这个:
$arrayl = json_decode(gettviewers($post->id, 10));
foreach($arrayl as $value1)
{
print $value1->content->firstName;
}
答案 2 :(得分:0)
Foreach'采取'对象或数组的每个元素。所以当你预先做好$value1
isn&t;
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循环。
答案 3 :(得分:-1)
要打印json_encoded数组,请执行此操作
print_r($array);
或只是做
declare @tt TABLE (
logId INT, iId INT,
dt DATETIME, atId INT
INSERT @tt (logId, iId,
dt, atId) values
(1, 1, '2017-01-01 09:00', 1),
(2, 1, '2017-01-01 11:00', 1),
(3, 1, '2017-01-01 14:00', 4),
(4, 1, '2017-01-01 16:00', 4),
(5, 1, '2017-01-01 20:00', 1),
(6, 1, '2017-01-01 21:00', 4),
(7, 1, '2017-01-02 09:00', 4),
(8, 2, '2017-01-02 10:00', 1),
(9, 1, '2017-01-02 11:00', 1),
(10, 1, '2017-01-02 14:00', 1),
(11, 2, '2017-01-02 15:00', 4),
(12, 1, '2017-01-02 16:00', 1),
(13, 1, '2017-01-02 17:00', 1),
(14, 1, '2017-01-02 18:00', 1),
(15, 2, '2017-01-02 15:00', 1)
Select s.logId startLogid, e.logId endLogId,
s.iID, s.dt startTime, e.dt endTime
from @tt s join @tt e
on e.logId =
(Select min(logId) from @tt
where iId = s.iID
and atId = 1
and logId > s.logId)
where s.aTid = 4
and ((Select atId from @tt
Where logId =
(Select Max(logId) from @tt
where logId < s.LogId
and iId = s.iId)) = 1
or Not Exists
(Select * from @tt
Where logId < s.LogId
and iId = s.iID))
解码后。