Creating a multidimensional associative array in php mysql

I am new to Php MySql and have a lot of answer in this question, but I still have an error. So what I'm trying to do is loop my array and create a multidimensional associative array.

I have rating, category, segment and answer tables. In the data_ rating methods, I have an identifier for categories, subcategories, segments and question tables

And in the category I have an is_cat column, and its data type is tiny int, either 0 or 1, which means 1 if this category is the main category and 0 if it is a subcategory, so I need to create an array like this.

Within a category, the multiple subcategory and each subcategory have one question. And each category can be grouped by segment.

array:2 [
  0 => array:4 [
    "id" => 2
    "title" => "second evaluation form"
    "emp_position" => "System Architecture"
    "first_segment_name" => array:2 [
      0 => {
        +"category_name" => array 2 [
            0 => {
                +"subcategory" => sub_1
                +"question" => question_1
            },

            1 => {
                +"subcategory" => sub_2
                +"question" => question_2
            }
        ]
      }

      1 => {
        +"category_name" => array 2 [
            0 => {
                +"subcategory" => sub_3
                +"question" => question_3
            },

            1 => {
                +"subcategory" => sub_4
                +"question" => question_4
            }
        ]
      }

    ]

    "second_segment_name" => array:2 [
      0 => {
        +"category_name" => array 3 [
            0 => {
                +"subcategory" => sub_5
                +"question" => question_5
            },

            1 => {
                +"subcategory" => sub_6
                +"question" => question_6
            }

            2 => {
                +"subcategory" => sub_7
                +"question" => question_7
            }
        ]
      }

    ]
  ]

]

And here is what I'm trying to do

public function getEvaluation($data){

    $query = DB::table(
        DB::raw("(

                SELECT 
                e.id, 
                e.title,
                p.name position,
                s.name segment
                FROM 
                evaluation_details e_d
                JOIN evaluation e ON e_d.evaluation_id = e.id
                JOIN segment s ON e_d.segment_id = s.id
                JOIN position p ON e.position_id = p.id
                WHERE e.position_id = 2
                ORDER BY segment

        ) As `a`
        ")
    )->get()->toArray();

    $query = json_decode(json_encode($query), true);


    foreach ($query as $index => $data) {
       $first_array[$index]['id'] = $data['id'];
       $first_array[$index]['title'] = $data['title'];
       $first_array[$index]['emp_position'] = $data['position'];

       $first_array[$index]['segments'] = DB::table('evaluation_details AS e_d')
            ->select('c.name AS category')
            ->join('category AS c', 'e_d.category_id', 'c.id')
            ->join('evaluation As e', 'e_d.evaluation_id', '=', 'e.id')
            ->join('segment AS s', 'e_d.segment_id', 's.id')
            ->where('e.position_id', '=', '2')
            ->get()->toArray();   

    }
    echo '<pre>';
    dd($first_array);exit;


}

* . *

    array:2 [
  0 => array:4 [
    "id" => 2
    "title" => "first evaluation form"
    "emp_position" => "System Architecture"
    "segments" => array:2 [
      0 => {#497
        +"category": "test"
      }
      1 => {#498
        +"category": "Quality & Dependibility"
      }
    ]
  ]

]

, - .

+4
1

category category name.

foreach.

foreach( $first_array[$index]['segments'] as $index2 => $segment){
    $category_name = $segment['category']; //This will output test and resplace the category key
    $first_array[$index]['segments'][$index2][$category_name] = DB::Table...;//Perform your subcategory query here just like what you did in segment.
    unset($first_array[$index]['segments'][$index2]['category']);//Delete the old
}

:

foreach ($query as $index => $data) {
       $first_array[$index]['id'] = $data['id'];
       $first_array[$index]['title'] = $data['title'];
       $first_array[$index]['emp_position'] = $data['position'];

       $first_array[$index]['segments'] = DB::table('evaluation_details AS e_d')
            ->select('c.name AS category')
            ->join('category AS c', 'e_d.category_id', 'c.id')
            ->join('evaluation As e', 'e_d.evaluation_id', '=', 'e.id')
            ->join('segment AS s', 'e_d.segment_id', 's.id')
            ->where('e.position_id', '=', '2')
            ->get()->toArray();   

    }

foreach ($query as $index => $data) {
   $first_array[$index]['id'] = $data['id'];
   $first_array[$index]['title'] = $data['title'];
   $first_array[$index]['emp_position'] = $data['position'];

   $first_array[$index]['segments'] = DB::table('evaluation_details AS e_d')
        ->select('c.name AS category')
        ->join('category AS c', 'e_d.category_id', 'c.id')
        ->join('evaluation As e', 'e_d.evaluation_id', '=', 'e.id')
        ->join('segment AS s', 'e_d.segment_id', 's.id')
        ->where('e.position_id', '=', '2')
        ->get()->toArray();   
    foreach( $first_array[$index]['segments'] as $index2 => $segment){
        $category_name = $segment['category']; //This will output test and resplace the category key
        $first_array[$index]['segments'][$index2][$category_name] = DB::Table...;//Perform your subcategory query here just like what you did in segment.
        unset($first_array[$index]['segments'][$index2]['category']);//Delete the old
    }
}
+1

Source: https://habr.com/ru/post/1688851/


All Articles