我学习PHP以及JSON和想不通,为什么我有以下的输出:
JSON结果:JSON给人怪异的输出在PHP
{
"Results": [
{
"user_id": "1"
},
{
"email": "[email protected]"
},
{
"first_name": "User"
},
{
"last_name": "One"
},
{
"password": "pass1"
},
{
"creation_date": null
},
{
"profile_type": "0"
},
{
"user_id": "2"
},
{
"email": "[email protected]"
},
{
"first_name": "User"
},
{
"last_name": "Two"
},
{
"password": "pass2"
},
{
"creation_date": null
},
{
"profile_type": "0"
}
]
}
PHP:
<?php
require_once 'config/db.php';
$sql = new db();
$conn = $sql->connect();
$query = isset($_GET['query']) ? mysql_real_escape_string($_GET['query']) : "";
if(!empty($query))
{
$qur = mysql_query($query);
$result = array();
while($r = mysql_fetch_assoc($qur))
{
extract($r);
foreach($r as $key => $value)
{
$result[] = array($key => $value);
}
$json = array("Results" => $result);
}
}
@mysql_close($conn);
/* Output header */
header('Content-type: application/json');
echo json_encode($json, JSON_PRETTY_PRINT);
?>
我的问题是,为什么它会将每个值分成新记录,我希望每个MySQL记录都是一个JSON记录。
有没有什么奇怪的,那是你要求的输出 –
你能解释这段代码有什么问题吗? – arti
您正在寻找元素数组。 –