dongsuo0517 2018-06-13 06:44
浏览 54
已采纳

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

This is my current array and i want to compare similar user_id and generate new array with all the related questions. Like as we can see in below example that User 1 have Question 1 and Question 2 in array and at next we can see that User 1 have Question 4 and Question 5, So i need to combine all the questions and want to generate user wise array.

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

)

Here is the example what i want exactly.

[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'
   )
 )

So any one can help me to generate array like above for all users.

I have tried with below code to find similar value and push it new array.

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

        }
    }

And below is the output after applying above code.

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条回答 默认 最新

  • doushan6692 2018-06-13 16:59
    关注

    You could loop array $items with the questions and first store the user_id to use that later to index in the $result array as the key.

    Then unset user_id so we are left with the questions only. Check if the key is already in use in $result. If it is not, create the array structure that you want for user_id and questions and add the first questions array.

    If the key already exists, merge the questions array with the existing one.

    At the end use array_map and array_unique to remove all the duplicate questions and use array_values to index the array numerically.

    $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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的代码运行
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败