为什么php json数组返回空值,即使它有第一行的值

巴拉哈先生

我正在获取 json 数据

$sql=mysqli_query($con,"select * FROM mohamiusermeta  where meta_key='websiteurl' or meta_key='profile_photo' or meta_key = 'Office' or meta_key='address_user' or meta_key='agentarea' or meta_key='offertext' or meta_key='officename' or meta_key='cover_photo' or meta_key='membertype' order by user_id desc");
$newarray = array();
$subArray = array();
while($row=mysqli_fetch_assoc($sql))
{
    $output[$row['meta_key']] = $row['meta_value'];
    $subArray[user_id]=$row['user_id'];
    $subArray[Office]=$output['Office']; 

    $subArray[officename]=$output['officename']; 
    $subArray[address_user]=$output['address_user']; 
    $subArray[profile_photo]=$output['profile_photo']; 
    $subArray[cover_photo]=$output['cover_photo']; 
    $subArray[agentarea]=$output['agentarea']; 
    $subArray[offertext]=$output['offertext']; 
    $subArray[websiteurl]=$output['websiteurl']; 
    $subArray[membertype]=$output['membertype']; 
    $newarray[] = $subArray;
}

json_encode($newarray);

echo(json_encode($newarray,JSON_UNESCAPED_UNICODE));

mysqli_close();
?>

但是浏览器中的结果在数据库中具有值的第一行中获得空值和重复值

获取数组有什么问题?要在浏览器中查看结果,请访问http://lifecareclub.net/api/test.php

特里科特

$newarray当您修改 的成员时,您正在修改下一次迭代中已添加的内容$subArray

为了解决这个问题,$subArray = array();在循环内部移动,就在第一次分配给它的一个键之前。这样你就可以在每次迭代中创建一个数组,它会从你在上一次迭代中分配给其成员的数据中“削减它”。

注意:您需要在$subArray['user_id'] = ...和其他作业中使用引号,但我认为这是您问题中的一个错字。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章