将php数组转换为另一种格式无法正常工作

时间:2016-11-03 10:13:11

标签: php json

我是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));

这不能给我正确的格式。

2 个答案:

答案 0 :(得分:1)

首先,让我们构建你的数组。

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

资源

答案 1 :(得分:0)

正如其他人已经指出的那样,您所需的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