如何使用 json 在 php 中解码从数据库中获取数据并显示如下结果?

How to fetch data from database using json decode in php and display the result like below?

我使用 JSON 将值从表单发送到数据库。我的 table(table tblstudent 的名字)有三列,即

    name                   score                               pic

["aa","bb","cc"]     ["525","523","562"]         ["img1.png","img2.png","img3.png"]

如何从 JSON 中获取值进行编码并将其放入 foreach 循环以获得类似

的结果
img1.png
aa
525

image2.png
bb
523

image3.png
cc
562

等等

可以使用for循环,通过key设置索引

  $name = ["aa","bb","cc"];
  $score = ["525","523","562"];
  $pic =  ["img1.png","img2.png","img3.png"];

  for($i = 0; $i < count($pic); $i++) {

    echo $pic[$i] . PHP_EOL;
    echo $name[$i] . PHP_EOL;
    echo $score[$i] . PHP_EOL;
    echo PHP_EOL;

  }

输出:

img1.png
aa
525

img2.png
bb
523

img3.png
cc
562

我尝试 index.php 文件发送 post 请求到 request.phprequest.php 文件发送数据到 json_decode 的形式并将数据打印到文件。

index.php

<!DOCTYPE html>
<html>
<head>
    <title>send request</title>
    <!--- jQuery cdn ----->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
    <div class="display-data"></div>
</body>
</html>
<script type='text/javascript'>
    
    $(document).ready(function () {
        $.post('request.php', function (data, status) {
            if (status == 'success') {
                $('.display-data').append(data);
            }
        })
    });
    
</script>

request.php

<?php

//This is a create a connection 
$servername = 'localhost';
$dbname = "Whosebug";  
$username = "root";
$pass = "";

$conn = new PDO("mysql:host=$servername; dbname=$dbname;", "$username", "$pass");

//This is a create a connection end


$selectSql = "SELECT * FROM tblstudent";
$select = $conn->prepare($selectSql);
$select->execute();

for($i=0; $i<3; $i++) {
    $data = $select->fetch();
    
    $img[] = $data['image']; //This is a array of image
    $name[] = $data['name']; //This is a array of name
    $score[] = $data['score']; //This is a array of score
}

for($i=0; $i<3; $i++) {
    
    echo $img[$i]."<br>"; //Print data to index.php file
    echo $name[$i].'<br>'; //Print data to index.php file
    echo $score[$i]."<br>"; //Print data to index.php file
    echo '<br><br>';
}

?>