Это мой текущий массив, и я хочу сравнить подобный user_id и сгенерировать новый массив со всеми связанными вопросами.Как видно из приведенного ниже примера, у пользователя 1 есть вопрос 1 и вопрос 2 в массиве, и на следующем этапе мы можем видеть, что у пользователя 1 есть вопрос 4 и вопрос 5, поэтому мне нужно объединить все вопросы и создать пользовательский массив.
Array
[0] => Array
(
[user_id] => User 1
[question_1] => Question 1?
[question_2] => Question 2?
[1] => Array
(
[user_id] => User 2
[question_1] => Question 2?
[question_2] => Question 3?
)
[2] => Array
(
[user_id] => User 3
[question_1] => Question 3?
[question_2] => Question 1?
)
[3] => Array
(
[user_id] => User 4
[question_1] => Question 1?
[question_2] => Question 2?
)
[4] => Array
(
[user_id] => User 5
[question_1] => Question 2?
[question_2] => Question 3?
)
[5] => Array
(
[user_id] => User 1
[question_1] => Question 4?
[question_2] => Question 5?
)
[6] => Array
(
[user_id] => User 2
[question_1] => Question 5?
[question_2] => Question 6?
)
[7] => Array
(
[user_id] => User 3
[question_1] => Question 6?
[question_2] => Question 4?
)
[8] => Array
(
[user_id] => User 4
[question_1] => Question 4?
[question_2] => Question 5?
)
[9] => Array
(
[user_id] => User 5
[question_1] => Question 5?
[question_2] => Question 6?
)
[10] => Array
(
[user_id] => User 1
[question_1] => Question 7?
[question_2] => Question 8?
)
[11] => Array
(
[user_id] => User 2
[question_1] => Question 8?
[question_2] =>
)
[12] => Array
(
[user_id] => User 3
[question_1] =>
[question_2] => Question 7?
)
[13] => Array
(
[user_id] => User 4
[question_1] => Question 7?
[question_2] => Question 8?
)
[14] => Array
(
[user_id] => User 5
[question_1] => Question 8?
[question_2] =>
)
)
Вот пример того, что я хочу именно.
[0] = Array
('user_id' => 'User 1',
'questions' => Array
(
'Question 1',
'Question 2',
'Question 4',
'Question 5',
'Question 7',
'Question 8'
)
)
[1] = Array
('user_id' => 'User 2',
'questions' => Array
(
'Question 2',
'Question 3',
'Question 5',
'Question 6',
'Question 8'
)
)
Так что любой может помочь мне сгенерироватьмассив, как указано выше, для всех пользователей.
Я попытался с помощью приведенного ниже кода найти аналогичное значение и выдвинуть в него новый массив.
foreach ($final_array as $master)
{
array_push($ques,$master['question_1']);
array_push($ques,$master['question_2']);
$temp_array = array('user_id' => $master['user_id'],'questions' => $ques);
$search = ['user_id' => $master['user_id']];
$keys1 = array_keys(array_filter($all_array,function ($v) use ($search) { return $v['user_id'] == $search['user_id']; } ) );
if(isset($keys1[0]))
{
$st = $keys1[0];
array_push($all_array[$st]['questions'],$master['question_1']);
array_push($all_array[$st]['questions'],$master['question_2']);
}
else
{
array_push($all_array, $temp_array);
}
}
И ниже вывод после применения приведенного выше кода.
Array
(
[0] => Array
(
[user_id] => User 1
[questions] => Array
(
[0] => Question 1?
[1] => Question 2?
[2] => Question 4?
[3] => Question 5?
[4] => Question 7?
[5] => Question 8?
)
)
[1] => Array
(
[user_id] => User 2
[questions] => Array
(
[0] => Question 1?
[1] => Question 2?
[2] => Question 2?
[3] => Question 3?
[4] => Question 5?
[5] => Question 6?
[6] => Question 8?
[7] =>
)
)
[2] => Array
(
[user_id] => User 3
[questions] => Array
(
[0] => Question 1?
[1] => Question 2?
[2] => Question 2?
[3] => Question 3?
[4] => Question 3?
[5] => Question 1?
[6] => Question 6?
[7] => Question 4?
[8] =>
[9] => Question 7?
)
)
[3] => Array
(
[user_id] => User 4
[questions] => Array
(
[0] => Question 1?
[1] => Question 2?
[2] => Question 2?
[3] => Question 3?
[4] => Question 3?
[5] => Question 1?
[6] => Question 1?
[7] => Question 2?
[8] => Question 4?
[9] => Question 5?
[10] => Question 7?
[11] => Question 8?
)
)
[4] => Array
(
[user_id] => User 5
[questions] => Array
(
[0] => Question 1?
[1] => Question 2?
[2] => Question 2?
[3] => Question 3?
[4] => Question 3?
[5] => Question 1?
[6] => Question 1?
[7] => Question 2?
[8] => Question 2?
[9] => Question 3?
[10] => Question 5?
[11] => Question 6?
[12] => Question 8?
[13] =>
)
)
)