比较数组中的类似值并使用其他值生成新数组

时间:2018-06-13 06:44:59

标签: php arrays multidimensional-array merge

这是我当前的数组,我想比较类似的user_id并生成包含所有相关问题的新数组。就像我们在下面的示例中看到的那样,用户1在数组中有问题1和问题2,在接下来我们可以看到用户1有问题4和问题5,所以我需要结合所有问题并希望生成用户明智的数组

阵列

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

        )

)

2 个答案:

答案 0 :(得分:0)

您可以使用用户ID列表($ userArr)创建另一个数组并循环原始标记数组($ questionArray)并检查用户ID数组。

$userArr = array();

// create array with user id list
foreach ($questionArray as $key1 => $outArr){ 
    foreach ($outArr as $key2 => $val){

        if($key2=='user_id'){
            $userArr[] = $val;
        }        
    }

}
$userArrUnique = array_unique($userArr);// make the array unique


foreach ($questionArray as  $key3 => $arr1){

    foreach ($userArrUnique as $key4 => $user){

        if($user == $arr1['user_id']){
            $finalArr[$key4]['user_id'] = $arr1['user_id'];
            $finalArr[$key4]['questions'][] = $arr1['question_1'];
            $finalArr[$key4]['questions'][] = $arr1['question_2'];
        }
    }
}

print_r($finalArr);

答案 1 :(得分:0)

您可以使用问题循环数组$items并首先存储user_id以便稍后使用$result数组中的索引作为键。

然后unset user_id,我们只留下问题。检查密钥是否已在$result中使用。如果不是,请为user_idquestions创建所需的数组结构,并添加第一个问题数组。

如果密钥已存在,请将问题数组与现有密钥合并。

最后使用array_maparray_unique删除所有重复的问题,并使用array_values以数字方式索引数组。

$result = [];

foreach ($items as $item) {
    $userId = $item['user_id'];
    unset($item['user_id']);
    if (!array_key_exists($userId, $result)) {
        $result[$userId] = [
            'user_id' => $userId,
            'questions' => array_values($item)
        ];
        continue;
    }
    $result[$userId]['questions'] = array_merge(
        array_values($result[$userId]['questions']),
        array_values($item)
    );
}

$result = array_values(array_map(function($y) {
    $y['questions'] = array_unique($y['questions']);
    return $y;
}, $result));

print_r($result);

Demo