将PHP数组转换为另一种格式不能正常工作


converting php array into another format not working properly

我是json中的新手。以下是我在php页面中运行sql查询得到的结果。

ID      Name  Crs_ID  C_name  Tchr_name Tchr_ID classID
1052    Ahd   2      Arabic     Nahd     78      17
1052    Ahd   15     English    Beatr    28      17
1545    Ayh   7      Arabic    Mohamed   75      37
1545    Ayh   20     English    Andrea   126     37

我需要这个结果以如下数组的形式出现:

[{
{            
    "ID":1052,  
    "Name":'Ahd',
    "classid":17,
    "results":[
           { 
             "Crs_ID" : "7", 
             "C_name  ":"Arabic",
             "Tchr_name ":"Nahd",
             "Tchr_ID ":"78"         
           },
            { 
             "Crs_ID" : "20", 
             "C_name  ":"English",
             "Tchr_name ":"Beatr",
             "Tchr_ID ":"28"         
           }
             ]
  },
  {
     "ID":1545,  
    "Name":'Ayh',
    "classid":37,
    "results":[
           { 
             "Crs_ID" : "2", 
             "C_name  ":"Arabic",
             "Tchr_name ":"Mohamed",
             "Tchr_ID ":"75"         
           },
            { 
             "Crs_ID" : "15", 
             "C_name  ":"English",
             "Tchr_name ":"Andrea",
             "Tchr_ID ":"126"        
           }
             ]
   }
  }
  ]

我使用了下面的代码来返回结果。

//$results has the sql results
$temp_array = array();
foreach($results as $key=>$array_data)
            {
                if(!in_array($array_data[ID],$temp_array))
                {
                    $temp_array['ID']   = $array_data['ID'];
                    $temp_array['Name'] = $array_data['Name'];
                    $temp_array['classID']     = $array_data['classID'];
                    $temp_array['results_'.$array_data[ID]] = array();
                    $result_array  =array();
                }
                else
                {
                    array_push($temp_array['results_'.$array_data[ID]],'Crs_ID:'.$array_data['Crs_ID']);
                    array_push($temp_array['results_'.$array_data[ID]],'C_name:'.$array_data['C_name']);
                    array_push($temp_array['results_'.$array_data[ID]],'Tchr_name:'.$array_data['Tchr_name']);
                    array_push($temp_array['results_'.$array_data[ID]],'Tchr_ID:'.$array_data['Tchr_ID']);   
                    $result_array[] = $temp_array['results_'.$array_data[ID]];
                }
            }
            print_r(json_encode($result_array));

首先,让我们构建数组。

$stmt = 'select ... from ...';
// Do your database logic here
$array = array();
while ($record = mysqli_fetch_assoc($resource)) {
    $array[] = $record;
}

PHP有json_encode()函数将数组转换为json。

// Convert array to json
echo json_encode($array);

将json转换回数组?

$array = json_decode($jsonString);
资源

  • json_encode() - Manual
  • json_decode() - Manual

正如其他人已经指出的,您想要的JSON是无效的。这是一个快速的&我认为你想要的:

// Names of all fields that you want inside your "results" sub-array
$result_fields  = explode(",", "Crs_ID,C_name,Tchr_name,Tchr_ID");
// The array that has to be built 
$new_arr = array();
//  First foreach loops over all rows
foreach ($arr as $row) {
    // ID of the current student (this will be the key for your new array)
    $studentID = $row['ID'];
    // result-array for this row
    $result     = array();
    // Second foreach loops over all elements in the current row 
    foreach ($row as $key => $value) {
        // Check whether the current element belongs inside the result-array
        if (in_array($key, $result_fields)) {
            $result[$key]   = $value;
        // If not, add it to the main array directly
        } else {
            $new_arr[$studentID][$key]  = $value;
        }
    }
    // When done with all elements of this row, add the result-array to the main array
    $new_arr[$studentID]['results'][]   = $result;
}
// This reindexes the array, so that you have a zero- and sequentially-indexed array
$new_arr = array_values($new_arr);
// Encode to JSON 
$json = json_encode($new_arr);

我希望评论能说清楚发生了什么。

小提琴:https://eval.in/671274