我是Php MySql的新手,在这个问题上有很多答案,但我仍然有错误。所以我要做的是循环我的数组并创建一个多维关联数组。
我有evaluate_details,类别,细分和问题表 在evaluation_details中,我有类别,子类别,细分和问题表的ID
在类别I中有列is_cat且其数据类型为tiny int,值为0或1,如果给定类别为主类别则为1,如果为子类别则为0,因此我需要创建数组像这样。
内部类别多个子类别和每个子类别都有一个问题。每个类别都可以按细分。
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"
}
]
]
]
我希望有人能帮助我。
答案 0 :(得分:0)
为此,我们需要删除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
}
}