Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按JSON数组对SQL数据进行分组_Php_Mysql_Json_Pdo_Highcharts - Fatal编程技术网

Php 按JSON数组对SQL数据进行分组

Php 按JSON数组对SQL数据进行分组,php,mysql,json,pdo,highcharts,Php,Mysql,Json,Pdo,Highcharts,我正在为教师制作一个页面,显示学生在考试中的成绩 要显示图形,我想使用HighchartsJavaScript库 到目前为止,我有一个PHP脚本,它使用PDO创建JSON数据,以后可以从不同的页面将这些数据馈送到Highcharts 我的问题: 如何将来自同一学生的所有数据分组到一个数组中?关于我希望实现的目标,请参见最后一个示例。另外:我希望将所有数据封装在一个总体JSON数组中 我想要这个: [{ "student": "Andreas", "level" : [4, 3] }, {

我正在为教师制作一个页面,显示学生在考试中的成绩

要显示图形,我想使用
Highcharts
JavaScript库

到目前为止,我有一个
PHP
脚本,它使用
PDO
创建JSON数据,以后可以从不同的页面将这些数据馈送到
Highcharts

我的问题: 如何将来自同一
学生的所有数据分组到一个数组中?关于我希望实现的目标,请参见最后一个示例。另外:我希望将所有数据封装在一个总体JSON数组中

我想要这个:

[{
  "student": "Andreas",
  "level" : [4, 3]
}, {
  "student": "Eivind",
  "level" : [4, 5]
}, {
  "student": "Ole",
  "level" : [4, 3]
}]
<?php

require("config.inc.php");

$school = $_GET["school"];
$class = $_GET["class"];

//initial query
$query = 'SELECT student, taskid, level FROM task
            WHERE school=' . '"' . $school . '"' .  ' AND class=' . '"' . $class . '" ORDER BY student';

//execute query
try {
    $stmt   = $db->prepare($query);
    $result = $stmt->execute();
}
catch (PDOException $ex) {
    $response["success"] = 0;
    $response["message"] = "Database Error!";
    die(json_encode($response));
}

// Finally, we can retrieve all of the found rows into an array using fetchAll 
$rows = $stmt->fetchAll();

if ($rows) {
    $response["posts"]   = array();

    foreach ($rows as $row) {
        $post               = array();
        $post["student"]       = $row["student"];
        $post["level"]         = $row["level"];


        //update our repsonse JSON data
        array_push($response["posts"], $post);
    }

    // echoing JSON response
    echo json_encode($response, JSON_NUMERIC_CHECK);


} else {
    $response["success"] = 0;
    $response["message"] = "No Post Available!";
    die(json_encode($response));
}

?>
{
  "posts": [
    {
      "student": "Andreas",
      "level": 4
    },
    {
      "student": "Andreas",
      "level": 3
    },
    {
      "student": "Eivind",
      "level": 4
    },
    {
      "student": "Eivind",
      "level": 5
    },
    {
      "student": "Ole",
      "level": 4
    },
    {
      "student": "Ole",
      "level": 3
    }
  ]
}
这就是我的PHP的样子:

[{
  "student": "Andreas",
  "level" : [4, 3]
}, {
  "student": "Eivind",
  "level" : [4, 5]
}, {
  "student": "Ole",
  "level" : [4, 3]
}]
<?php

require("config.inc.php");

$school = $_GET["school"];
$class = $_GET["class"];

//initial query
$query = 'SELECT student, taskid, level FROM task
            WHERE school=' . '"' . $school . '"' .  ' AND class=' . '"' . $class . '" ORDER BY student';

//execute query
try {
    $stmt   = $db->prepare($query);
    $result = $stmt->execute();
}
catch (PDOException $ex) {
    $response["success"] = 0;
    $response["message"] = "Database Error!";
    die(json_encode($response));
}

// Finally, we can retrieve all of the found rows into an array using fetchAll 
$rows = $stmt->fetchAll();

if ($rows) {
    $response["posts"]   = array();

    foreach ($rows as $row) {
        $post               = array();
        $post["student"]       = $row["student"];
        $post["level"]         = $row["level"];


        //update our repsonse JSON data
        array_push($response["posts"], $post);
    }

    // echoing JSON response
    echo json_encode($response, JSON_NUMERIC_CHECK);


} else {
    $response["success"] = 0;
    $response["message"] = "No Post Available!";
    die(json_encode($response));
}

?>
{
  "posts": [
    {
      "student": "Andreas",
      "level": 4
    },
    {
      "student": "Andreas",
      "level": 3
    },
    {
      "student": "Eivind",
      "level": 4
    },
    {
      "student": "Eivind",
      "level": 5
    },
    {
      "student": "Ole",
      "level": 4
    },
    {
      "student": "Ole",
      "level": 3
    }
  ]
}
您将获得以下输出:

[{"category":"Search Engines","hits":13,"bytes":673684},
 {"category":"Content Server","hits":3,"bytes":88930},
 {"category":"Internet Services","hits":1,"bytes":3690},
 {"category":"Business","hits":1,"bytes":2847}]

在php中,您可以执行以下操作:

在你的面前

foreach ($rows as $row) {
    $post               = array();
    $post["student"]       = $row["student"];
    $post["level"]         = $row["level"];


    //update our repsonse JSON data
    array_push($response["posts"], $post);
}
您需要这样做:

<?php
$response = array();
$response["posts"]   = array();

$students = array("student1", "student2", "student3", "student1");

$tempArray = array();

foreach ($students as $student) {
    $lvl        = 1;
    if(!isset($tempArray[$student])){
              $tempArray[$student] =  array("name" => $student, "level" => array($lvl));
    }
    else{
        $tempArray[$student]["level"][] = $lvl;
    }
}
    // add it to the array
   $response["posts"] = $tempArray;

   // encode array
   $response = json_encode($response);

   echo "<br><br><br> ";

// example

// decode and make it a array for easier looping.
$response = (array)json_decode($response);
$responsePosts = (array) $response["posts"];

// foreach post
foreach($response["posts"] as $keyP => $valueP){

        // convert to array same as above
        $valueP = (array) $valueP;


        // key that is kinda useless but made it the student name so i can easily see if it already exists
        echo "key :".$keyP." <br />";

        // name of the student
        echo "name :".$valueP["name"]." <br />";

        // all the levels
        echo "levels: ";
        foreach($valueP["level"] as $lvl){
            echo $lvl." ";
        }

        echo "<br /><br />";
}


?>

这应该行得通。Atm我使用学生名作为一个键,以查看它是否已经存在,否则您需要遍历所有数组并执行“name”=$student最有可能的操作


不需要推送。

通过使用学生姓名作为数组键,您可以非常轻松地构建这样的数组。构建数组后,可以使用将字符串键转换回数字键

...
if ($rows) {
    foreach ($rows as $row) {
        $posts[$row['student']]['student'] = $row['student'];
        $posts[$row['student']]['level'][] = $row['level'];
    }
    $response = array_values($posts);

    echo json_encode($response, JSON_NUMERIC_CHECK);

} else { ...
这将为您提供以下
$response

[
  {
    "student": "Andreas",
    "level": [4, 3]
  },
  {
    "student": "Eivind",
    "level": [4, 5]
  },
  {
    "student": "Ole",
    "level": [4, 3]
  }
]

谢谢我添加了
array\u push($response[“posts”],$tempArray)
在foreach的
末尾,我得到的结果是:接近,但还没有到达。。如果我不在
foreach
的末尾执行
array\u push
,JSON数据返回器将为空。我将看看是否可以在我的示例中包含push,并使其与您要求的完全相同。我推荐@Prashant给我的答案,如果你能做到的话。这个应该可以。要获得所需的精确数组,可以执行var_dump($response[“posts”][0]);数组推送创建了一个键0,我现在不知道如何删除它。我有点傻。我删除了数组并将其分配给了数组。这正是你要找的。你在最近几次编辑中把我弄丢了。。需要对´´$students´´进行硬编码,因此,如果这些数据是由实际学生生成的,则没有任何用处:-(另外,在你最新的方法中,我没有得到JSON编码的数据,只是得到了一些PHP转储。谢谢你的努力,但到目前为止编辑#2似乎最接近。这太棒了!谢谢你。