操纵ResultSet数组并使其成为多维

时间:2015-06-12 08:25:58

标签: php arrays multidimensional-array

我正在开发一个基于Left outer join从三个表中获取数据的函数,如下所示:

public function get_full_category_history($id) {
        $id = mysqli_real_escape_string($this->_con,$id);
        if (isset($id) && $id != "") {
            $query = "SELECT qz_categories.id as category_id, qz_categories.name as category_name, qz_quizzes.id as quiz_id, qz_quizzes.name as quiz_name, qz_questions.id as question_id, qz_questions.question as question FROM qz_categories LEFT OUTER JOIN qz_quizzes ON qz_categories.id = qz_quizzes.category_id LEFT OUTER JOIN qz_questions ON qz_quizzes.id = qz_questions.quiz_id WHERE ( qz_categories.id = '".$id."')";
        }
        $categoriesList = mysqli_query($this->_con, $query) or die(mysqli_error($this->_con));

        $count = $categoriesList->num_rows;
        $result = '';
        $i = 0;
        if ($count > 0) {
            while($row = mysqli_fetch_array($categoriesList)){
                $result[$i]['category_id'] = $row['category_id'];
                $result[$i]['category_name'] = $row['category_name'];
                $result[$i]['quiz_id'] = $row['quiz_id'];
                $result[$i]['quiz_name'] = $row['quiz_name'];
                $result[$i]['question_id'] = $row['question_id'];
                $result[$i]['question'] = $row['question'];
                $i++;
            }
            return $result;
        }else{
                return FALSE;
            }
    }

这样做后我得到了这个数组:

Array
(
    [0] => Array
        (
            [category_id] => 1
            [category_name] => Preschool
            [quiz_id] => 1
            [quiz_name] => What is Forex?
            [question_id] => 1
            [question] => In forex, what are the three main types of analysis?
        )

    [1] => Array
        (
            [category_id] => 1
            [category_name] => Preschool
            [quiz_id] => 1
            [quiz_name] => What is Forex?
            [question_id] => 2
            [question] => Which type of analysis looks at historical price movements to determine the current trading conditions?
        )

    [2] => Array
        (
            [category_id] => 1
            [category_name] => Preschool
            [quiz_id] => 2
            [quiz_name] => Why Trade Forex?
            [question_id] => 
            [question] => 
        )

)

但我想要的是:

Array
(
    [0] => Array
        (
            [category_id] => 1
            [category_name] => Preschool
            ['Preschool']=>Array(
                0=> array(
                            [quiz_id] => 1
                            [quiz_name] => What is Forex?
                            [What is Forex?]=>Array(
                                    0=>array(
                                                [question_id] => 1
                                                [question] => In forex, what are the three main types of analysis?
                                    ),
                                    1=>array(
                                                [question_id] => 2
                                                [question] => Which type of analysis looks at historical price movements to determine the current trading conditions?
                                    )
                                )
                ),
                1=>array(
                            [quiz_id] => 2
                            [quiz_name] => Why Trade Forex?
                            [Why Trade Forex?]=>Array(

                                )
                )
        )
    )
);

怎么做?

3 个答案:

答案 0 :(得分:3)

检查一下:

它会根据您的需要提供相同的输出: 并且它不会添加空问题数组

我还添加了另一个"category_id" => 2,它将被添加到主数组的下一个索引中。

$array = Array
(
    0 => Array
        (
            "category_id" => 1,
            "category_name" => 'Preschool',
            "quiz_id" => 1,
            "quiz_name" => 'What is Forex?',
            "question_id" => 1,
            "question" => 'In forex, what are the three main types of analysis?',
        ),
    3 => Array
        (
            "category_id" => 2,
            "category_name" => 'Preschool2',
            "quiz_id" => 1,
            "quiz_name" => 'What is Forex?',
            "question_id" => 1,
            "question" => 'In forex, what are the three main types of analysis?',
        ),

    1 => Array
        (
            "category_id" => 1,
            "category_name" => 'Preschool',
            "quiz_id" => 1,
            "quiz_name" => 'What is Forex?',
            "question_id" => 2,
            "question" => 'Which type of analysis looks at historical price movements to determine the current trading conditions?',
        ),

    2 => Array
        (
            "category_id" => 1,
            "category_name" => 'Preschool',
            "quiz_id" => 2,
            "quiz_name" => 'Why Trade Forex?',
            "question_id" => '',
            "question" => ''
        )

);  




  function myfun($array){
$result= array();   
$insertTo = 0;
foreach($array as $key=>$value){
    $qArray = array('question_id' => $value['question_id'],'question' => $value['question']);
    if(!empty($result)){
        $check=false; 
        foreach($result as $k=>$v){
            if($value['category_id'] == $v['category_id']){
                if($value['question_id'] != '' && $value['question_id'] != ''){
                    $result[$k][$v['category_name']][$value['quiz_name']][]=$qArray;                
                }
                $check=true; 
            }
        }   
    if(!$check){
        $insertTo = count($result);
        $result[$insertTo]['category_id'] = $value['category_id'];
        $result[$insertTo]['category_name'] = $value['category_name'];
        $result[$insertTo][$value['category_name']]['quiz_id'] = $value['quiz_id'];
        $result[$insertTo][$value['category_name']]['quiz_name'] = $value['quiz_name'];
        if($value['question_id'] != '' && $value['question_id'] != ''){
            $result[$insertTo][$value['category_name']][$value['quiz_name']][] = $qArray;
        }
    }       
    }else{
        $result[$insertTo]['category_id'] = $value['category_id'];
        $result[$insertTo]['category_name'] = $value['category_name'];
        $result[$insertTo][$value['category_name']]['quiz_id'] = $value['quiz_id'];
        $result[$insertTo][$value['category_name']]['quiz_name'] = $value['quiz_name'];
        if($value['question_id'] != '' && $value['question_id'] != ''){
            $result[$insertTo][$value['category_name']][$value['quiz_name']][] = $qArray;
        }
    }

}
return $result;
}
    $result = myfun($array);
    echo '<pre>';
    print_r($result); 

<强>输出

enter image description here

答案 1 :(得分:2)

这可能对您有所帮助,我使用您的for line in f数组作为输入,我知道您可以轻松使用它。

$result

输出

 [akshay@localhost tmp]$ cat test.php
 <?php

 $result = array (
   0 => 
   array (
     'category_id' => '1',
     'category_name' => 'Preschool',
     'quiz_id' => '1',
     'quiz_name' => 'What is Forex?',
     'question_id' => '1',
     'question' => 'In forex, what are the three main types of analysis?',
   ),
   1 => 
   array (
     'category_id' => '1',
     'category_name' => 'Preschool',
     'quiz_id' => '1',
     'quiz_name' => 'What is Forex?',
     'question_id' => '2',
     'question' => 'Which type of analysis looks at historical price movements to determine the current trading conditions?',
   ),
   2 => 
   array (
     'category_id' => '1',
     'category_name' => 'Preschool',
     'quiz_id' => '2',
     'quiz_name' => 'Why Trade Forex?',
     'question_id' => '',
     'question' => false,
   ),
 );

 $output = array();

 foreach( $result as $index => $row)
 {
    $inner_2 = array('question_id'=>$row['question_id'],'question'=>$row['question']);
    $inner_1 = array('quiz_id'=>$row['quiz_id'],'quiz_name'=>$row['quiz_name'], $row['quiz_name']=> array($inner_2));

    if(isset($output[$row['category_name']]))
    {
        $key = array_search($row['quiz_id'],array_column($output[$row['category_name']][$row['category_name']],"quiz_id"));

        if( $key !== false )
        {
            $output[$row['category_name']][$row['category_name']][$key][$row['quiz_name']][] = $inner_2; 
        }else
        {
            $output[$row['category_name']][$row['category_name']][] = $inner_1 ;
        }

    }else
    {
       $output[$row['category_name']] = array('category_id'=>$row['category_id'],'category_name'=> $row['category_name'],$row['category_name']=>array($inner_1));
    }
 }

 // Input 
 print_r($result);

 // Output
 print_r(array_values($output));

 ?>

- 对于评论(因为可能是旧版本的php) -

 [akshay@localhost tmp]$ php test.php
 Array
 (
     [0] => Array
         (
             [category_id] => 1
             [category_name] => Preschool
             [quiz_id] => 1
             [quiz_name] => What is Forex?
             [question_id] => 1
             [question] => In forex, what are the three main types of analysis?
         )

     [1] => Array
         (
             [category_id] => 1
             [category_name] => Preschool
             [quiz_id] => 1
             [quiz_name] => What is Forex?
             [question_id] => 2
             [question] => Which type of analysis looks at historical price movements to determine the current trading conditions?
         )

     [2] => Array
         (
             [category_id] => 1
             [category_name] => Preschool
             [quiz_id] => 2
             [quiz_name] => Why Trade Forex?
             [question_id] => 
             [question] => 
         )

 )
 Array
 (
     [0] => Array
         (
             [category_id] => 1
             [category_name] => Preschool
             [Preschool] => Array
                 (
                     [0] => Array
                         (
                             [quiz_id] => 1
                             [quiz_name] => What is Forex?
                             [What is Forex?] => Array
                                 (
                                     [0] => Array
                                         (
                                             [question_id] => 1
                                             [question] => In forex, what are the three main types of analysis?
                                         )

                                     [1] => Array
                                         (
                                             [question_id] => 2
                                             [question] => Which type of analysis looks at historical price movements to determine the current trading conditions?
                                         )

                                 )

                         )

                     [1] => Array
                         (
                             [quiz_id] => 2
                             [quiz_name] => Why Trade Forex?
                             [Why Trade Forex?] => Array
                                 (
                                     [0] => Array
                                         (
                                             [question_id] => 
                                             [question] => 
                                         )

                                 )

                         )

                 )

         )

 )

Demo

答案 2 :(得分:2)

  

可以做这个最短的方法

     

输入&amp;代码

 $array = Array
(
    0 => Array
        (
            "category_id" => 1,
            "category_name" => 'Preschool',
            "quiz_id" => 1,
            "quiz_name" => 'What is Forex?',
            "question_id" => 1,
            "question" => 'In forex, what are the three main types of analysis?',
        ),
    3 => Array
        (
            "category_id" => 2,
            "category_name" => 'Preschool2',
            "quiz_id" => 1,
            "quiz_name" => 'What is Forex?',
            "question_id" => 1,
            "question" => 'In forex, what are the three main types of analysis?',
        ),

    1 => Array
        (
            "category_id" => 1,
            "category_name" => 'Preschool',
            "quiz_id" => 1,
            "quiz_name" => 'What is Forex?',
            "question_id" => 2,
            "question" => 'Which type of analysis looks at historical price movements to determine the current trading conditions?',
        ),

    2 => Array
        (
            "category_id" => 1,
            "category_name" => 'Preschool',
            "quiz_id" => 2,
            "quiz_name" => 'Why Trade Forex?',
            "question_id" => '',
            "question" => ''
        )

);  

$modifiedArray = array();
    foreach($array as $key=>$value){
        if(!isset($modifiedArray[$value['category_id']])){
            $modifiedArray[$value['category_id']] = array("category_id"=>$value['category_id'],"category_name"=>$value['category_name']);
        }
        if(!isset($modifiedArray[$value['category_id']][$value['category_name']][$value['quiz_id']])){
            $modifiedArray[$value['category_id']][$value['category_name']][$value['quiz_id']] = array('quiz_id'=>$value['quiz_id'],'quiz_name'=>$value['quiz_name']);
        }
        if(!isset($modifiedArray[$value['category_id']][$value['category_name']][$value['quiz_id']][$value['quiz_name']][$value['question_id']])){
            $modifiedArray[$value['category_id']][$value['category_name']][$value['quiz_id']][$value['quiz_name']][$value['question_id']] = array('question_id'=>$value['question_id'],'question'=>$value['question']);
        }
    }
    print_r($modifiedArray);die;
  

输出

Array
(
    [1] => Array
        (
            [category_id] => 1
            [category_name] => Preschool
            [Preschool] => Array
                (
                    [1] => Array
                        (
                            [quiz_id] => 1
                            [quiz_name] => What is Forex?
                            [What is Forex?] => Array
                                (
                                    [1] => Array
                                        (
                                            [question_id] => 1
                                            [question] => In forex, what are the three main types of analysis?
                                        )

                                    [2] => Array
                                        (
                                            [question_id] => 2
                                            [question] => Which type of analysis looks at historical price movements to determine the current trading conditions?
                                        )

                                )

                        )

                    [2] => Array
                        (
                            [quiz_id] => 2
                            [quiz_name] => Why Trade Forex?
                            [Why Trade Forex?] => Array
                                (
                                    [] => Array
                                        (
                                            [question_id] => 
                                            [question] => 
                                        )

                                )

                        )

                )

        )

    [2] => Array
        (
            [category_id] => 2
            [category_name] => Preschool2
            [Preschool2] => Array
                (
                    [1] => Array
                        (
                            [quiz_id] => 1
                            [quiz_name] => What is Forex?
                            [What is Forex?] => Array
                                (
                                    [1] => Array
                                        (
                                            [question_id] => 1
                                            [question] => In forex, what are the three main types of analysis?
                                        )

                                )

                        )

                )

        )

)