在 php 中创建动态 JSON 数组

Posted

技术标签:

【中文标题】在 php 中创建动态 JSON 数组【英文标题】:Create dynamic JSON array in php 【发布时间】:2020-12-05 18:59:25 【问题描述】:

我想像这样动态创建一个 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")];

只有一条记录时有效,多条记录时无效。 有人可以帮我吗?

【问题讨论】:

警告:您对SQL Injections 持开放态度,应该使用参数化的prepared statements,而不是手动构建查询。它们由PDO 或MySQLi 提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your data。 Escaping is not enough! 【参考方案1】:

只需替换:

$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");

【讨论】:

【参考方案2】:

那是因为你在 while 中使用了 $response['storie'] 来解决这个问题:

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;

【讨论】:

以上是关于在 php 中创建动态 JSON 数组的主要内容,如果未能解决你的问题,请参考以下文章

如何在aspx中创建一个json数组

在 C++ 中创建动态对象的动态数组

在节点 js 中创建 JSON 数组

在 Swift 中创建 JSON 数组

在Objective C中创建JSON数组[重复]

jquery/json - 为键创建动态数组