php - 使用 php 将多维数组从多个表转换为单个 json 数组值

标签 php arrays json mysqli

在我的 PHP 脚本中,尝试使用多维数组执行单个 json 文件中多个表中的数据。

所以我将用我的代码解释:

if($data){    
            $sql="select * from en_providers where providerEmailAddress='".$email."' and providerPW='".$password."'";
            $result = mysqli_query($con,$sql) or die("Error in Selecting " . mysqli_error($connection));
            if (mysqli_num_rows($result) > 0) {
                 $resultArray = array();
             while ($row = mysqli_fetch_assoc($result)) {
            $providerID = $row['providerID'];
            $resultArray['providers'] = $row; 

            $sql1="select * from en_venues where providerID = $providerID ";
            $result1 = mysqli_query($con,$sql1) or die("Error in Selecting " . mysqli_error($connection));
             $i=0;
        while ($row1['venue'][] = mysqli_fetch_assoc($result1)){
        echo $venueID=$row1['venue'][$i++]['venueID'];
        $resultArray['venues'] = $row1; 


            $sql2 = "select * from en_schedules as e ,en_persons as p where e.venueID = '".$venueID."'";
            $result2 = mysqli_query($con,$sql2) or die("Error in Selecting " . mysqli_error($con));
            while ($row2['Persons'][] = mysqli_fetch_assoc($result2)){
                $resultArray['Persons'] = $row2; 
            }  
}
}
echo json_encode($resultArray);
}

输出变得像

{
"providers": {
             "providerID": "1",
             "providerEmailAddress": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4c25222a230c2b2838292222253f622f23623927" rel="noreferrer noopener nofollow">[email protected]</a>",
             "providerPW": "xxx",
             "providerName": "GDT Tennis ",
             "providerActive": "yes",
             "providerFeatured": "no",
           },
"venues": {
     "venue": {
           "venueID": "8",
           "providerID": "1",
           "venueActive": "yes",
           "venueName": "Wargrave LTC ",
              }
 }
"Persons": {
"Persons": [
  {
"scheduleID": "1",
"venueID": "151",
"scheduleTitle": "Mini Tennis Red ",
"scheduleDay": "Monday",
},
{
"scheduleID": "1",
"venueID": "151",
"scheduleTitle": "Mini Tennis yellow ",
"scheduleDay": "Monday",
},
{
"scheduleID": "1",
"venueID": "152",
"scheduleTitle": "Mini Tennis orange ",
"scheduleDay": "sunday",
}
],
}
}

在 field 数组中,我实际上只得到一个数组,它有 7 个数组值。但它只得到一个我不知道为什么这样。

请帮我解决问题

最佳答案

你刚刚犯了一个错误,只需将 $row1['venue'] 更改为 $row1['venue'][]

 $i=0;
  while ($row1['venue'][] = mysqli_fetch_assoc($result1)){    
    $venueID=$row1['venue'][$i++]['venueID'];
    }

关于php - 使用 php 将多维数组从多个表转换为单个 json 数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44044695/

相关文章:

php - XML 到数据库 - foreach 循环到 MySQL

php - JQuery $.ajax "async: false"错误?

php - 为什么这个 for 循环不起作用(为什么最后一个索引没有显示值)

C++:二维数组作为类成员

javascript - 我的 JSON 数据仅填充到一个 ul 元素中,而不是每个 ul 元素中

php - 使用Services PHP/Mysql Xamarin登录失败

php - Codeigniter 中的 is_home() 函数

c++ - ref类c++中的动态多维数组

java - ObjectMapper.readValue 可以返回空值吗?

java - 一些数组操作