2017-11-07 14 views
4

私はPhp MySqlで新しく、この質問には多くの回答がありますが、これでもまだエラーがあります。だから私がやろうとしているのは、配列をループし、多次元連想配列を作成することです。 evaluation_details、カテゴリ、セグメントおよび質問表のevaluation_detailsで を持っている私は、私は、カテゴリ、サブカテゴリ、セグメントと質問テーブルのIDphp mysqlで多次元連想配列を作成する

を持っているカテゴリに、私は列is_catとそのデータ型を持っている

は小さなintで値は0または1です。これは、指定されたカテゴリがメインカテゴリの場合は1を意味し、サブカテゴリの場合は0を意味するため、このような配列を作成する必要があります。

内部カテゴリ複数のサブカテゴリと各サブカテゴリには1つの質問があります。各カテゴリはセグメントごとにグループ化できます。

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

    ] 
    ] 

] 

そして、これは私が

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" 
     } 
    ] 
    ] 

] 

I内部の別の配列を持っている必要があります異なる結果を与えます誰もがこれで私を助けることを願っています。

+0

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

をあなたが直面している問題は何ですか? –

+0

私は混乱しています。投稿したコードは何ですか?これを多次元配列に変換しますか?またはこのような多次元配列を作成したいですか? – Bluetree

+0

@MayankPandeyz私はそのような配列を作成したいとします –

答えて

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($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 
    } 
} 
+0

ありがとうございます。私はあなたにこれに謝っています –

+0

あなたは歓迎です。私はエラーを修正してうれしいです。 – Bluetree

+0

あなたが私を助けたから:)))) –

関連する問題