【问题标题】:PHP Mysql Select multiple table, and make multidimensional arrayPHP Mysql 选择多个表,并制作多维数组
【发布时间】:2017-04-17 19:48:17
【问题描述】:

我有 4 张桌子。

学生(id_student,姓名)

主题(id_subject,subject_name)

subject_category(id_subject_category、id_subject、subject_category)

分数(id_score、id_student、id_subject、id_subject_category、score)

public function score(){
    $this->db->select('*');
    $this->db->from('student');
    $query0 = $this->db->get();
    $data=$query0->result_array();
    $j = 0;


    while($j<count($data)){ 
        $sql2= "SELECT * FROM score where score.id_student = ?";  
        $data2 = $this->db->query($sql2,$data[$j]['id_student']);        
        $data2 =  $data2->result_array();
        $data[$j]['score'] = $data2;
        $j++;
        }
    return $data;  
}

结果是

Array
(
    [final] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [name] => John
                    [score] => Array
                        (
                            [0] => Array
                                (
                                    [id_score] => 1
                                    [id_student] => 1
                                    [id_subject] => 2
                                    [id_subject_category] => 1
                                    [score] => 90
                                )
                            [1] => Array
                                (
                                    [id_score] => 2
                                    [id_student] => 1
                                    [id_subject] => 2
                                    [id_subject_category] => 2
                                    [score] => 70
                                )
                            [2] => Array
                                (
                                    [id_score] => 3
                                    [id_student] => 1
                                    [id_subject] => 2
                                    [id_subject_category] => 3
                                    [score] => 60
                                )


                        )

                )

        )

)

我需要做这样的数组

Array
(
    [final] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [name] => John
                    [score] => Array
                        (
                            [id_subject] => Array
                                (
                                    [0] => Array
                                        (
                                           [id_score] => 1
                                           [id_student] => 1
                                           [id_subject_category] => 1
                                           [score] => 90
                                        )
                                    [2] => Array
                                        (
                                           [id_score] => 2
                                           [id_student] => 1
                                           [id_subject_category] => 2
                                           [score] => 70
                                        )
                                    [3] => Array
                                        (
                                           [id_score] => 3
                                           [id_student] => 1
                                           [id_subject_category] => 3
                                           [score] => 60
                                        )

                                )

                        )

                )

        )

)

【问题讨论】:

  • 我认为只是键对的那些有什么区别?

标签: php arrays codeigniter multidimensional-array model


【解决方案1】:

试试这个

public function score(){
$this->db->select('*');
$this->db->from('student');
$query0 = $this->db->get();
$data=$query0->result_array();
$j = 0;


while($j<count($data)){ 
    $sql2= "SELECT * FROM score where score.id_student = ?";  
    $data2 = $this->db->query($sql2,$data[$j]['id_student']);        
    $data2 =  $data2->result_array();
    $data[$j]['score'][$data[$j]['id_student']] = $data2;
    $j++;
    }
return $data;  
}

【讨论】:

    猜你喜欢
    • 2017-06-14
    • 1970-01-01
    • 2017-11-11
    • 2011-02-26
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多