2017-04-17 76 views
0

我有4个表。PHP Mysql选择多个表,并制作多维数组我有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,得分)

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 
             ) 

           ) 

         ) 

       ) 

     ) 

) 
+0

什么那些之间的区别,我认为只是钥匙吧? – Omi

回答

0

阵列试试这个

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