0

I am working on a function which fetch data from three table based on Left outer join, which is as follows:

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

After doing this I got this 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?
        )

    [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] => 
        )

)

But what I want is:

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(

                                )
                )
        )
    )
);

How to do that?

3
  • array without index ??? just look after What is Forex? Commented Jun 12, 2015 at 8:37
  • sorry let me edit it. thanks for telling. Commented Jun 12, 2015 at 8:44
  • upvoted to cancel out the drive-by downvote Commented Jun 12, 2015 at 11:50

3 Answers 3

2
+100

Check this:

It will give you the same output as you require: and it will not add empty question array

I have also added another "category_id" => 2 and it will be added to the next index of main array.

$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); 

Output

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

2

This may help you, I am using your $result array as input, I know you can easily play with this.

 [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));

 ?>

Output

 [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] => 
                                         )

                                 )

                         )

                 )

         )

 )

-- For comments (since may be older version of php)--

if(!function_exists("array_column"))
{
    function array_column($array,$column_name)
    {
        return array_map(function($element) use($column_name){return $element[$column_name];}, $array);
    }
}

Demo

6 Comments

Fatal error: Call to undefined function array_column() in D:\xampp\htdocs\quiz\lib\quiz.class.php on line 1322
Mostly you are using old php please paste this function and try
Can you tell me any modification ,because it's running on live server and upgradation is quite lengthy.
I up voted because it will work for latest version, but not in my case.thanks.
As you said i already did that before modification (create a function just like you), but weared condition that it still shows same fetal error. i tried your code on higher version and since it works that why i up-voted you,but it's not the ultimate solution for me. thanks again for your effort.
|
2

Can do like this shortest method

Input & Code

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

OutPUT

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

                                )

                        )

                )

        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.