php json_encode数组访问

时间:2014-03-06 18:52:09

标签: php json

我想从查询字符串id获取json请求它几乎可以工作,但它正在添加 一些额外的数组对象

$id = $_GET['id']; 
$result = mysqli_query($con,'SELECT * FROM ContactInfo WHERE id =' . $id );
$row = mysqli_fetch_array($result);
echo json_encode($row);

{"0":"terry","FirstName":"terry","1":"rihoff","LastName":"rieff","2":"alientory","website":"alieory","3":"`123","PhoneNumber":"`123","4":"123","Fax":"123","5":"2","id":"2"}

我应该只获得一个联系人,但看起来像是要添加到每个字段的exrtra数组

1 个答案:

答案 0 :(得分:1)

mysqli_fetch_array()返回一个包含数字和字符串键的数组,因此您可以获得两次数据(一次使用数字索引,一次使用字符串索引)。尝试:

$row = mysqli_fetch_assoc($result);

或者:

$row = mysqli_fetch_array($result, MYSQLI_ASSOC);