PHP操纵关联数组

时间:2015-09-22 03:06:28

标签: php

我正在使用PHP进行练习,这是为了获得用户的必要细节和他/她所做的测验。

到目前为止,这是我的代码。

‰PNG


IHDR   y   2     Þ  R    IDATx í]mLTYš~Øl€I ÃF  ´šX ‰|Dc H(œ    TÿàcÒÅPkuÂGÿ°è B'*ì: èHÖ    =ÂàØà¸É”ýc¨ùa &–M75i„ ]0ÓR&’¢W£`RZ› ÅA·
LÒ`²¹›s?ªî­ïÂ[x©{ªÛÔ­óñž÷}žûž{îåT=i×JJ ÐWJ# Æ\ %9¥) þ!Åã£á ’¬Š“€f²
h¦$S’U€€
B¤™LIV  * ‘f2%Y  ¨ DšÉ”d   ‚ i&S’U€€
B¤™LIV  * ‘f2%Y  ¨ DšÉ”d   ‚ i&S’U€€
B¤™LIV  * ‘f2%Y  ¨ DšÉ”d   ‚ i&S’U€€
B¤™LIV  * ‘f2%Y  ¨ DšÉ”d   ‚ i&S’U€€
B¤™LIV  * 1 a ú;^)N4 ®Sœ`  %™’¬  T "ÍdJ²
 PAˆ4“)É*@@ !ÒL¦$«   „H3™’¬  T "ÍdJ²
 PAˆ4“)É*@@ !ÒL¦$«   „H3™’¬  T "ÍdJ²
 PAˆ4“)É*@@ !ÒL¦$‹ pc -

阵列操控性差,这就是我得到这个结果的原因。

<?php

    require("dbOption/Db.class.php");

$db = new Db();

$data = array();
$quiz = array();
$easy = "";
$normal = "";
$hard = "";

//  SELECT id, course FROM quizz WHERE course = 2
$res_quizz = $db->query("SELECT id, course FROM quizz WHERE course = :course", array("course"=>2));
foreach ($res_quizz as $key => $value) {
    $quizzid = $value['id'];

    //  SELECT easy, normal, hard,  userid FROM quizzscore WHERE quizid = $quizid
    $res_quizzscore = $db->query("SELECT easy, normal, hard, userid, quizzid FROM quizzscore WHERE quizzid = :quizzid", array("quizzid"=>$quizzid));

    foreach ($res_quizzscore as $key => $value2) {
        $easy = $value2['easy'];
        $normal = $value2['normal'];
        $hard = $value2['hard'];
        $quizzid = $value2['quizzid'];
        $userid = $value2['userid'];

        $q = array(
            'Qz'.$quizzid => array(
                    'easy' =>$easy,
                    'normal' =>$normal,
                    'hard' =>$hard,
                )
        );

        $quiz['quizzes'] = $q;

        //  SELECT name FROM USER WHERE id = $userid
        $res_user = $db->query("SELECT name FROM user WHERE id = :id", array("id"=>$userid));
        foreach ($res_user as $key => $value3) {
            $name = $value3['name'];
        }

    }

    $data[$userid] = array();
    $data[$userid]['name'] = $name;
    $data[$userid]['quizzes'] = $quiz;

    echo "<pre>";
    print_r($data);
    echo "<pre/>";
}

我想要的是这种结果。

Array
(
    [1] => Array
        (
            [name] => John Smith
            [quizzes] => Array
                (
                    [quizzes] => Array
                        (
                            [Qz1] => Array
                                (
                                    [easy] => 5
                                    [normal] => 6
                                    [hard] => 7
                                )

                        )

                )

        )

)

Array
(
    [1] => Array
        (
            [name] => John Smith
            [quizzes] => Array
                (
                    [quizzes] => Array
                        (
                            [Qz2] => Array
                                (
                                    [easy] => 3
                                    [normal] => 4
                                    [hard] => 5
                                )

                        )

                )

        )

)

任何想法都会受到最高的赞赏。

0 个答案:

没有答案