为了账号安全,请及时绑定邮箱和手机立即绑定

在 php 中创建动态 JSON 数组

在 php 中创建动态 JSON 数组

PHP
汪汪一只猫 2023-10-15 15:06:44
我想像这样动态创建一个 JSON:{"storie":[{"story_id":"111","username":"Username1","profile_photo":"boh.png","copertina":"ok.png","num_elements":"1","rating":"4.5"},{"story_id":"222","username":"Username2","profile_photo":"hello.png","copertina":"hi.png","num_elements":"2","rating":"3.5"}]}我正在尝试从 MySQL 数据库获取值,并且我能够做到这一点:$response = array();$sql = mysqli_query($conn, "SELECT * FROM storie WHERE userid IN (SELECT following FROM follow WHERE follower='$userid')");while($row = mysqli_fetch_assoc($sql)){  $usern = getuserinfo($row['userid'], "username", $conn);  $prof_photo = getuserinfo($row['userid'], "profile_photo", $conn);  $idsto=$row['storia_id'];  $elem = mysqli_query($conn, "SELECT COUNT(*) AS da_vedere FROM `storie_images` WHERE storia_id='$idsto' AND imm_id NOT IN (SELECT imm_id FROM image_views WHERE viewer='$userid')");  while($ok = mysqli_fetch_assoc($elem)){    $num_elem = $ok['da_vedere'];  }  //here I put the line that add the array to the json array:}但问题是这一行,它应该创建一个新数组并将其放入 json 中:$response['storie'] = [array("story_id" => $idsto, "username" => $usern, "profile_photo" => $prof_photo, "copertina" => $row['copertina'], "num_elements" => $num_elem, "rating" => "4.5")];当只有一条记录时它有效,但如果有更多记录它就不起作用。有人能帮我吗?
查看完整描述

2 回答

?
拉风的咖菲猫

TA贡献1995条经验 获得超2个赞

只需替换:

$response['storie'] = [array("story_id" => $idsto, "username" => $usern, "profile_photo" => $prof_photo, "copertina" => $row['copertina'], "num_elements" => $num_elem, "rating" => "4.5")];

$response['storie'][] = array("story_id" => $idsto, "username" => $usern, "profile_photo" => $prof_photo, "copertina" => $row['copertina'], "num_elements" => $num_elem, "rating" => "4.5");


查看完整回答
反对 回复 2023-10-15
?
呼啦一阵风

TA贡献1802条经验 获得超6个赞

那是因为您在 while 内使用 $response['story'] 来解决此问题:


1-创建另一个名为 result 的数组,然后在 while 中使用它


$response = array();

$result = array ()

while (....)

{

//here I put the line that add the array to the json array:

$result[] = array("story_id" => $idsto, "username" => $usern, "profile_photo" => $prof_photo, "copertina" => $row['copertina'], "num_elements" => $num_elem, "rating" => "4.5");

2-然后在循环外使用响应数组:


$response['storie'] = $result;

你的代码将是这样的:


   $response = array();

   $result = array ()


    $sql = mysqli_query($conn, "SELECT * FROM storie WHERE userid IN (SELECT following FROM follow WHERE follower='$userid')");

    while($row = mysqli_fetch_assoc($sql)){

    $usern = getuserinfo($row['userid'], "username", $conn);

    $prof_photo = getuserinfo($row['userid'], "profile_photo", $conn);

    $idsto=$row['storia_id'];

    $elem = mysqli_query($conn, "SELECT COUNT(*) AS da_vedere FROM `storie_images` WHERE storia_id='$idsto' AND imm_id NOT IN (SELECT imm_id FROM image_views WHERE viewer='$userid')");

    while($ok = mysqli_fetch_assoc($elem)){

    $num_elem = $ok['da_vedere'];

    }

    //here I put the line that add the array to the json array:

    $result[] = array("story_id" => $idsto, "username" => $usern, "profile_photo" => $prof_photo, "copertina" => $row['copertina'], "num_elements" => $num_elem, "rating" => "4.5");

    }


   $response['storie'] = $result;


查看完整回答
反对 回复 2023-10-15
  • 2 回答
  • 0 关注
  • 74 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信