M
M
masterit152018-02-08 11:54:24
PHP
masterit15, 2018-02-08 11:54:24

How to filter an array in php to remove duplicates and combine the value of unique keys into one line?

Good day to all, I'm desperate, I need help. There was a need to parse json by reference, actually I received the data as an array, they look like this

Array
(
    [0] => Array
        (
            [ID] => 105653877
            [CABLAB_NAME] => Участок
            [DEPARTMENT_NAME] => Отдел
            [DEPARTMENT_KIND] => 6405222
            [EMPLOYER_ID] => 78621962
            [EMPLOYER_NAME] => Иванов И. И.
            [SERVICE_ID] => 
            [SERVICE_NAME] => 
            [DEVICE_NAME] => 
            [DAY_NUMBER] => 1
            [HOURS_BEGIN] => 08
            [MINS_BEGIN] => 00
            [HOURS_END] => 14
            [MINS_END] => 00
            [TIME_BEGIN_S] => 08:00
            [TIME_END_S] => 14:00
            [TIME_TYPE] => 6338504
            [TIME_TYPE_CODE] => 1
            [RCOUNT] => 1
            [RCOUNTMAX] => 
            [TIME_OR_COUNT] => 0
            [RTIME_PRIM] => 15
            [SORT_F] => 2
            [ONLY_VIEW] => 0
        )

    [1] => Array
        (
            [ID] => 105653877
            [CABLAB_NAME] => Участок
            [DEPARTMENT_NAME] => Отдел
            [DEPARTMENT_KIND] => 6405222
            [EMPLOYER_ID] => 78621962
            [EMPLOYER_NAME] => Иванов И. И.
            [SERVICE_ID] => 
            [SERVICE_NAME] => 
            [DEVICE_NAME] => 
            [DAY_NUMBER] => 3
            [HOURS_BEGIN] => 08
            [MINS_BEGIN] => 00
            [HOURS_END] => 14
            [MINS_END] => 00
            [TIME_BEGIN_S] => 08:00
            [TIME_END_S] => 14:00
            [TIME_TYPE] => 6338504
            [TIME_TYPE_CODE] => 1
            [RCOUNT] => 1
            [RCOUNTMAX] => 
            [TIME_OR_COUNT] => 0
            [RTIME_PRIM] => 15
            [SORT_F] => 2
            [ONLY_VIEW] => 0
        )

    [2] => Array
        (
            [ID] => 105653877
            [CABLAB_NAME] => Участок
            [DEPARTMENT_NAME] => Отдел
            [DEPARTMENT_KIND] => 6405222
            [EMPLOYER_ID] => 78621962
            [EMPLOYER_NAME] => Иванов И. И.
            [SERVICE_ID] => 
            [SERVICE_NAME] => 
            [DEVICE_NAME] => 
            [DAY_NUMBER] => 5
            [HOURS_BEGIN] => 08
            [MINS_BEGIN] => 00
            [HOURS_END] => 14
            [MINS_END] => 00
            [TIME_BEGIN_S] => 08:00
            [TIME_END_S] => 14:00
            [TIME_TYPE] => 6338504
            [TIME_TYPE_CODE] => 1
            [RCOUNT] => 1
            [RCOUNTMAX] => 
            [TIME_OR_COUNT] => 0
            [RTIME_PRIM] => 15
            [SORT_F] => 2
            [ONLY_VIEW] => 0
        )

so what is the actual complexity, I need to print so that the duplicates are removed, and the value of the key that differ is written in one line in this way
Array
(
    [0] => Array
        (
            [ID] => 105653877
            [CABLAB_NAME] => Участок
            [DEPARTMENT_NAME] => Отдел
            [DEPARTMENT_KIND] => 6405222
            [EMPLOYER_ID] => 78621962
            [EMPLOYER_NAME] => Иванов И. И.
            [SERVICE_ID] => 
            [SERVICE_NAME] => 
            [DEVICE_NAME] => 
            [DAY_NUMBER] => 1, 3, 5
            [HOURS_BEGIN] => 08
            [MINS_BEGIN] => 00
            [HOURS_END] => 14
            [MINS_END] => 00
            [TIME_BEGIN_S] => 08:00
            [TIME_END_S] => 14:00
            [TIME_TYPE] => 6338504
            [TIME_TYPE_CODE] => 1
            [RCOUNT] => 1
            [RCOUNTMAX] => 
            [TIME_OR_COUNT] => 0
            [RTIME_PRIM] => 15
            [SORT_F] => 2
            [ONLY_VIEW] => 0
        )

maybe the task is easy, but I'm not friends with php, in general, I surfed the forums looking for examples and nothing, thank you in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nick Sdk, 2018-02-08
@masterit15

If you only need to look at 'DAY_NUMBER', then something like this:

Our array
$arr = [
    [
        'ID' => '105653877',
        'CABLAB_NAME' => 'Участок',
        'DEPARTMENT_NAME' => 'Отдел',
        'DEPARTMENT_KIND' => '6405222',
        'EMPLOYER_ID' => '78621962',
        'EMPLOYER_NAME' => 'Иванов И. И.',
        'SERVICE_ID' => '',
        'SERVICE_NAME' => '',
        'DEVICE_NAME' => '',
        'DAY_NUMBER' => '1',
        'HOURS_BEGIN' => '08',
        'MINS_BEGIN' => '00',
        'HOURS_END' => '14',
        'MINS_END' => '00',
        'TIME_BEGIN_S' => '08:00',
        'TIME_END_S' => '14:00',
        'TIME_TYPE' => '6338504',
        'TIME_TYPE_CODE' => '1',
        'RCOUNT' => '1',
        'RCOUNTMAX' => '',
        'TIME_OR_COUNT' => '0',
        'RTIME_PRIM' => '15',
        'SORT_F' => '2',
        'ONLY_VIEW' => '0',
    ],
    [
        'ID' => '105653877',
        'CABLAB_NAME' => 'Участок',
        'DEPARTMENT_NAME' => 'Отдел',
        'DEPARTMENT_KIND' => '6405222',
        'EMPLOYER_ID' => '78621962',
        'EMPLOYER_NAME' => 'Иванов И. И.',
        'SERVICE_ID' => '',
        'SERVICE_NAME' => '',
        'DEVICE_NAME' => '',
        'DAY_NUMBER' => '3',
        'HOURS_BEGIN' => '08',
        'MINS_BEGIN' => '00',
        'HOURS_END' => '14',
        'MINS_END' => '00',
        'TIME_BEGIN_S' => '08:00',
        'TIME_END_S' => '14:00',
        'TIME_TYPE' => '6338504',
        'TIME_TYPE_CODE' => '1',
        'RCOUNT' => '1',
        'RCOUNTMAX' => '',
        'TIME_OR_COUNT' => '0',
        'RTIME_PRIM' => '15',
        'SORT_F' => '2',
        'ONLY_VIEW' => '0',
    ],
    [
        'ID' => '105653877',
        'CABLAB_NAME' => 'Участок',
        'DEPARTMENT_NAME' => 'Отдел',
        'DEPARTMENT_KIND' => '6405222',
        'EMPLOYER_ID' => '78621962',
        'EMPLOYER_NAME' => 'Иванов И. И.',
        'SERVICE_ID' => '',
        'SERVICE_NAME' => '',
        'DEVICE_NAME' => '',
        'DAY_NUMBER' => '5',
        'HOURS_BEGIN' => '08',
        'MINS_BEGIN' => '00',
        'HOURS_END' => '14',
        'MINS_END' => '00',
        'TIME_BEGIN_S' => '08:00',
        'TIME_END_S' => '14:00',
        'TIME_TYPE' => '6338504',
        'TIME_TYPE_CODE' => '1',
        'RCOUNT' => '1',
        'RCOUNTMAX' => '',
        'TIME_OR_COUNT' => '0',
        'RTIME_PRIM' => '15',
        'SORT_F' => '2',
        'ONLY_VIEW' => '0',
    ]
];
$new_arr = [];
foreach ($arr as $index => $item) {
    $id = $item['ID'];
    $new_arr[$id] = $new_arr[$id] ?? $item;
    $day = $item['DAY_NUMBER'];
    $days = explode(', ', $new_arr[$id]['DAY_NUMBER']);
    if (!in_array($day, $days))
        $days[] = $day;
    sort($days);
    $new_arr[$id]['DAY_NUMBER'] = implode(', ', $days);
}

M
masterit15, 2018-02-08
@masterit15

close, but they are also repeated and it adds the previous value to each next duplicate, that is

Array
(
    [0] => Array
        (
            [ID] => 105653877
            [CABLAB_NAME] => Участок
            [DEPARTMENT_NAME] => Отдел
            [DEPARTMENT_KIND] => 6405222
            [EMPLOYER_ID] => 78621962
            [EMPLOYER_NAME] => Иванов И. И.
            [SERVICE_ID] => 
            [SERVICE_NAME] => 
            [DEVICE_NAME] => 
            [DAY_NUMBER] => 1, 3, 5
            [HOURS_BEGIN] => 08
            [MINS_BEGIN] => 00
            [HOURS_END] => 14
            [MINS_END] => 00
            [TIME_BEGIN_S] => 08:00
            [TIME_END_S] => 14:00
            [TIME_TYPE] => 6338504
            [TIME_TYPE_CODE] => 1
            [RCOUNT] => 1
            [RCOUNTMAX] => 
            [TIME_OR_COUNT] => 0
            [RTIME_PRIM] => 15
            [SORT_F] => 2
            [ONLY_VIEW] => 0
        )

    [1] => Array
        (
            [ID] => 105653877
            [CABLAB_NAME] => Участок
            [DEPARTMENT_NAME] => Отдел
            [DEPARTMENT_KIND] => 6405222
            [EMPLOYER_ID] => 78621962
            [EMPLOYER_NAME] => Иванов И. И.
            [SERVICE_ID] => 
            [SERVICE_NAME] => 
            [DEVICE_NAME] => 
            [DAY_NUMBER] => 1, 3
            [HOURS_BEGIN] => 08
            [MINS_BEGIN] => 00
            [HOURS_END] => 14
            [MINS_END] => 00
            [TIME_BEGIN_S] => 08:00
            [TIME_END_S] => 14:00
            [TIME_TYPE] => 6338504
            [TIME_TYPE_CODE] => 1
            [RCOUNT] => 1
            [RCOUNTMAX] => 
            [TIME_OR_COUNT] => 0
            [RTIME_PRIM] => 15
            [SORT_F] => 2
            [ONLY_VIEW] => 0
        )

    [2] => Array
        (
            [ID] => 105653877
            [CABLAB_NAME] => Участок
            [DEPARTMENT_NAME] => Отдел
            [DEPARTMENT_KIND] => 6405222
            [EMPLOYER_ID] => 78621962
            [EMPLOYER_NAME] => Иванов И. И.
            [SERVICE_ID] => 
            [SERVICE_NAME] => 
            [DEVICE_NAME] => 
            [DAY_NUMBER] => 1, 3, 5
            [HOURS_BEGIN] => 08
            [MINS_BEGIN] => 00
            [HOURS_END] => 14
            [MINS_END] => 00
            [TIME_BEGIN_S] => 08:00
            [TIME_END_S] => 14:00
            [TIME_TYPE] => 6338504
            [TIME_TYPE_CODE] => 1
            [RCOUNT] => 1
            [RCOUNTMAX] => 
            [TIME_OR_COUNT] => 0
            [RTIME_PRIM] => 15
            [SORT_F] => 2
            [ONLY_VIEW] => 0
        )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question