gpt4 book ai didi

php - 循环数组没有给出所需的结果

转载 作者:太空宇宙 更新时间:2023-11-03 12:05:11 25 4
gpt4 key购买 nike

我正在编写一个脚本,它从一个表 (release_dates) 中获取信息,但也从另一个表 (release_screenshots) 中获取包含 X 个屏幕截图的列表。然后它将所有信息放入一个数组并将其编码为 JSON。

但是,每个 JSON 条目都有完全相同的屏幕截图列表。

这是我的代码片段:

$json_response = array();

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$row_array['id'] = $row['id'];
$row_array['game_title'] = $row['game_title'];
$row_array['game_platform'] = $row['game_platform'];
$row_array['game_genre'] = $row['game_genre'];
$row_array['game_publisher'] = $row['game_publisher'];
$row_array['release_eu'] = $row['release_eu'];
$row_array['release_us'] = $row['release_us'];
$row_array['rrp_gbp'] = $row['rrp_gbp'];
$row_array['rrp_eur'] = $row['rrp_eur'];
$row_array['rrp_usd'] = $row['rrp_usd'];
$row_array['link_address'] = $row['link_address'];
$row_array['logo_image'] = $row['logo_image'];
$row_array['box_art'] = $row['box_art'];

//Build incrementing variable name
$ssno = 1;
$ss = "ss_".$ssno;

//Get the list of screenshots where the referring ID is = to the current ID
$query2 = "SELECT * FROM release_screenshots WHERE parent_id = '$row_array[id]'";
$result2 = mysql_query($query2);
while($row2 = mysql_fetch_array($result2)){
$array[] = $row2;
}

//Add each Screenshot link to the array build and increment the variable name
foreach($array as $x){

$row_array[$ss] = $x['link'];
$ssno = $ssno + 1;
$ss = "ss_".$ssno;
}

$row_array['youtube_link'] = $row['youtube_link'];
$row_array['notes'] = $row['notes'];
$row_array['entry_created'] = $row['entry_created'];


array_push($json_response,$row_array);
}
echo json_encode($json_response);

Tables

我希望我已经解释清楚了。如果没有,我可以提供更多信息。任何帮助将不胜感激。

最佳答案

您需要在第二个while 循环之前清除$array。否则,您将添加到之前游戏的数组中。

$query2 = "SELECT * FROM release_screenshots WHERE parent_id = '$row_array[id]'";
$result2 = mysql_query($query2);
$array = array();
while($row2 = mysql_fetch_array($result2)){
$array[] = $row2;
}

您还需要在外部 while 循环的开头清除 $row_array:

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$row_array = array();
...

关于php - 循环数组没有给出所需的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26938236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com