我正在跟踪来自$jsonobj = json_decode($result);
当我print_r($jsonobj);
我得到了
stdClass Object (
[flag] => stdClass Object (
[flag] => true
)
[records] => stdClass Object (
[news_id] => 1
[news_date] => 2015-07-04
[heading] => sdf
[details] => sdf
[dstatus] => 0
)
)
我想要做的是,如果记录存在于数据库中,则第一个对象成立,否则为false,现在我想在json中循环抛出this [records]
对象。打印值如何做到?\
源码我正在尝试编码数组
$sql = "select * from latest_news where dstatus=0";
$stmt = $con->prepare($sql);
$stmt->execute();
$rows_found = $stmt->rowCount();
if($rows_found)
{
$json_data['flag']=array('flag'=>'true');
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
$json_data['records']=$row;
}
header('Content-Type: application/json');
print(json_encode($json_data));
$con->commit();
}
else
{
$con->rollBack();
$json_data['flag']=array('flag'=>'false');
header('Content-Type: application/json');
print(json_encode($json_data));
}
答案 0 :(得分:0)
$jsonobj = json_decode($result);
你会得到
stdClass Object (
[flag] => stdClass Object (
[flag] => true
)
[records] => stdClass Object (
[news_id] => 1
[news_date] => 2015-07-04
[heading] => sdf
[details] => sdf
[dstatus] => 0
)
)
您可以使用
进行检查if($jsonobj->flag->flag == true){}
尝试
print_r($jsonobj->flag->flag);
如果您想获得记录,可以尝试使用
print_r($jsonobj->records);
因为json_decode($ result);将获得对象结果,您也可以尝试学习
var_dump(json_decode($result));
var_dump(json_decode($result, true));
请参阅This Link
答案 1 :(得分:0)
您创建records
元素的代码不正确。每次循环都会覆盖$json_data['records']
,而不是将另一行推到它上面。要获取所有行的数组,请使用:
$json_data['records'] = $stmt->fetchAll(PDO::FETCH_ASSOC);
然后,当您解码JSON时,您可以循环遍历行:
foreach ($jsonobj->records as $record) {
echo $record->news_id;
}