按JSON数组

时间:2015-11-16 11:59:18

标签: php mysql json pdo highcharts

我正在为教师编制一个显示学生成绩的页面。考试中的表现。

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

到目前为止,我有一个使用PHP的{​​{1}}脚本来创建JSON数据,以后可以从另一个页面将其提供给PDO

我的问题: 如何对数组中相同Highcharts的所有数据进行分组?请参阅最后一个示例,了解我希望实现的目标。另外:我希望将所有数据都包含在一个总体JSON数组中。

我想要这个:

student

这就是我的PHP的样子:

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

这是我从PHP获得的:

<?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));
}

?>

3 个答案:

答案 0 :(得分:2)

var data=[ 
 { "category" : "Search Engines", "hits" : 5, "bytes" : 50189 },
 { "category" : "Content Server", "hits" : 1, "bytes" : 17308 },
 { "category" : "Content Server", "hits" : 1, "bytes" : 47412 },
 { "category" : "Search Engines", "hits" : 1, "bytes" : 7601 },
 { "category" : "Business", "hits" : 1, "bytes" : 2847 },
 { "category" : "Content Server", "hits" : 1, "bytes" : 24210 },
 { "category" : "Internet Services", "hits" : 1, "bytes" : 3690 },
 { "category" : "Search Engines", "hits" : 6, "bytes" : 613036 },
 { "category" : "Search Engines", "hits" : 1, "bytes" : 2858 } 
];

var res = alasql('SELECT category, sum(hits) AS hits, sum(bytes) as bytes \
FROM ? \
GROUP BY category \
ORDER BY bytes DESC',[data]);

您将获得输出:

[{"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}]

答案 1 :(得分:1)

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

...
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]
  }
]

答案 2 :(得分:0)

在php中你可以这样做:

在你的foreach中

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 />";
}


?>

这应该有效。我使用学生名作为键来查看它是否已经存在,否则你需要循环遍历所有数组并做一个&#34; name&#34; === $学生最有可能。

不需要推动。