如何从API中提取每个JSON返回对象前面的id
?
电流:
{
"1516":{
"id":"1516",
"firstname":"Aluno",
"lastname":"Teste",
"email":"teste@gmail.com",
"dlastaccess":"28-10-2016",
"coursename":"Curso Demonstra\u00e7\u00e3o"
}
}
我想如何离开:
[
{
"id":"1516",
"firstname":"Aluno",
"lastname":"Teste",
"email":"teste@gmail.com",
"dlastaccess":"28-10-2016",
"coursename":"Curso Demonstra\u00e7\u00e3o"
}
]
我正在尝试这样做,因为我的清单模型不能像JSON返回那样工作,所以我无法检查所有复选框。
API:
答案 0 :(得分:1)
这就是工作:
$json = '{"1516":{"id":"1516","firstname":"Aluno","lastname":"Teste","email":"teste@gmail.com","dlastaccess":"28-10-2016","coursename":"Curso Demonstra\u00e7\u00e3o"}}';
$values = json_decode($json, true);
$values = array_values($values);
echo json_encode($values);
返回:
[{"id":"1516","firstname":"Aluno","lastname":"Teste","email":"teste@gmail.com","dlastaccess":"28-10-2016","coursename":"Curso Demonstra\u00e7\u00e3o"}]
答案 1 :(得分:0)
我猜你的$ result是这种类型的数组(或对象):
$ result [id] = array(检索到sql);
因为sql会返回结果列表,即使结果是唯一的。想象一下,您的查询返回了两个结果,您如何将它们分开?
尝试:
echo json_encode($ result [$ cursoid])
或
echo json_encode($ result-> $ cursoid)
答案 2 :(得分:0)
$cursoid = $_GET['idcurso'];
$sql = 'SELECT
user2.id AS ID,
user2.firstname AS Firstname,
user2.lastname AS Lastname,
user2.email AS Email,
IF (user2.lastaccess = 0,"nunca",
DATE_FORMAT(FROM_UNIXTIME(user2.1astaccess),"%d-96m-W")) AS dLastAccess ,c.fullname AS Coursename
FROM mdl_user_enrolments AS ue
JOIN mdl_enrol AS e ON e.id = ue.enrolid
JOIN mdl_course AS c ON c.id = e.courseid
JOIN mdl_user AS user2 ON user2 .id = ue.userid
WHERE (SELECT timeaccess FROM mdl_user_lastaccess WHERE userid=user2.id AND courseid=c.id) IS NULL and c.id = ?';
$params = array($cursoid);
$result = $DB->get_records_sql($sql, $params);
echo json_encode(array_values($result)); // You can easily access array values without their keys by array_values(array $param) function
答案 3 :(得分:0)
使用array_values,默认php函数
$values = $DB->get_records_sql( $sql, $params );
$result = array_values( $values );
return json_encode($result);