DWQA QuestionsCategory: Artificial IntelligenceHow to merge the data in the key of PHP two-dimensional array
º_z asked 3 weeks ago

Array
(
[categories] => Array
(
[0] => 22-01-30
[1] => 22-01-31
[2] => 22-02-01
[3] => 22-02-02
[4] => 22-02-03
[5] => 22-02-04
[6] => 22-02-05
[7] => 22-02-06
[8] => 22-02-07
[9] => 22-02-08
[10] => 22-02-09
[11] => 22-02-10
[12] => 22-02-11
[13] => 22-02-12
[14] => 22-02-13
[15] => 22-02-14
[16] => 22-02-15
[17] => 22-02-16
[18] => 22-02-17
[19] => 22-02-18
[20] => 22-02-19
[21] => 22-02-20
[22] => 22-02-21
[23] => 22-02-22
[24] => 22-02-23
[25] => 22-02-24
[26] => 22-02-25
[27] => 22-02-26
[28] => 22-02-27
[29] => 22-02-28
)[data] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
[10] => 5638.04
[11] => 0.00
[12] => 0.00
[13] => 0.00
[14] => 0.00
[15] => 0.00
[16] => 0.00
[17] => 0.00
[18] => 0.00
[19] => 0.00
[20] => 0.00
[21] => 0.00
[22] => 0.00
[23] => 0.00
[24] => 25915.99
[25] => 1606.44
[26] => 1676.90
[27] => 0.00
[28] => 0.00
[29] => 1203.08
))Merge the above arrays with the specified number (6 by default) to obtain the following arrays. Less than 6 normal dates are displayed, and more than 6 need to be merged
Array
(
[categories] => Array
(
[0] => 22-01-30/22-02-04
[1] => 22-02-05/22-02-10
[2] => 22-02-11/22-02-16
[3] => 22-02-17/22-02-22
[4] => 22-02-23/22-02-28
)[data] => Array
(
[0] => 0.00
[1] => 5638.04
[2] => 0.00
[3] => 0.00
[4] => 30402.41
)),array_ chunk + array_ Reduce and deal with it

<?php

$arr = [
    'categories' => [
        '22-01-30',
        '22-01-31',
        '22-02-01',
        '22-02-02',
        '22-02-03',
        '22-02-04',
        '22-02-05',
        '22-02-06',
        '22-02-07',
        '22-02-08',
        '22-02-09',
        '22-02-10',
        '22-02-11',
        '22-02-12',
        '22-02-13',
        '22-02-14',
        '22-02-15',
        '22-02-16',
        '22-02-17',
        '22-02-18',
        '22-02-19',
        '22-02-20',
        '22-02-21',
        '22-02-22',
        '22-02-23',
        '22-02-24',
        '22-02-25',
        '22-02-26',
        '22-02-27',
        '22-02-28',
    ],
    'data'       => [
        '0',
        '0',
        '0',
        '0',
        '0',
        '0',
        '0',
        '0',
        '0',
        '0',
        '5638.04',
        '0.00',
        '0.00',
        '0.00',
        '0.00',
        '0.00',
        '0.00',
        '0.00',
        '0.00',
        '0.00',
        '0.00',
        '0.00',
        '0.00',
        '0.00',
        '25915.99',
        '1606.44',
        '1676.90',
        '0.00',
        '0.00',
        '1203.08',
    ],

];

$chunk = 6;

$result = array_reduce(array_chunk($arr['categories'], $chunk), function ($result, $current) use ($chunk, $arr) {
    $start = current($current);
    $end = end($current);
    $result['categories'][] = $start . '/' . $end;

    $count = count($result['categories'] ?? []);
    $result['data'][] = array_sum(array_slice($arr['data'] ?? [], $count * $chunk, $chunk));

    return $result;
}, ['categories' => [], 'data' => []]);

var_dump($result, array_sum($arr['data']) === array_sum($result['data']));


1 Answers
Unique answered 3 weeks ago

array_ chunk + array_ Reduce and deal with it

<?php

$arr = [
    'categories' => [
        '22-01-30',
        '22-01-31',
        '22-02-01',
        '22-02-02',
        '22-02-03',
        '22-02-04',
        '22-02-05',
        '22-02-06',
        '22-02-07',
        '22-02-08',
        '22-02-09',
        '22-02-10',
        '22-02-11',
        '22-02-12',
        '22-02-13',
        '22-02-14',
        '22-02-15',
        '22-02-16',
        '22-02-17',
        '22-02-18',
        '22-02-19',
        '22-02-20',
        '22-02-21',
        '22-02-22',
        '22-02-23',
        '22-02-24',
        '22-02-25',
        '22-02-26',
        '22-02-27',
        '22-02-28',
    ],
    'data'       => [
        '0',
        '0',
        '0',
        '0',
        '0',
        '0',
        '0',
        '0',
        '0',
        '0',
        '5638.04',
        '0.00',
        '0.00',
        '0.00',
        '0.00',
        '0.00',
        '0.00',
        '0.00',
        '0.00',
        '0.00',
        '0.00',
        '0.00',
        '0.00',
        '0.00',
        '25915.99',
        '1606.44',
        '1676.90',
        '0.00',
        '0.00',
        '1203.08',
    ],

];

$chunk = 6;

$result = array_reduce(array_chunk($arr['categories'], $chunk), function ($result, $current) use ($chunk, $arr) {
    $start = current($current);
    $end = end($current);
    $result['categories'][] = $start . '/' . $end;

    $count = count($result['categories'] ?? []);
    $result['data'][] = array_sum(array_slice($arr['data'] ?? [], $count * $chunk, $chunk));

    return $result;
}, ['categories' => [], 'data' => []]);

var_dump($result, array_sum($arr['data']) === array_sum($result['data']));


º_z replied 3 weeks ago

Thank you for your ideas$count = count($result[‘categories’] ?? []);$result[‘data’][] = array_sum(array_slice($arr[‘data’] ?? [], ($count-1) * $chunk, $chunk));This meets my requirements