如何在PHP中执行分组[重复]

i86rm4rw  于 2023-09-29  发布在  PHP
关注(0)|答案(1)|浏览(109)

此问题已在此处有答案

Group data in a multidimensional array based on two columns(4个答案)
Group 2d array by column, preserve multiple other column values and push 2 column values into subarrays(4个答案)
How to group subarray data using multiple column values and individually sum the remaining column values?(3个答案)
Group array rows by column value and create subarrays in each group(4个答案)
Group array rows by column value and push whole rows as subarrays within the group(2个答案)
6天前关闭
我有下面的数组,其中,我有一个项目'CONSECUTIVO_GRUPO',我想作为一个引用来创建一组'RESPONSES'在一个数组中的三个位置,CODIGO,ID_MODULO,VALOR。初始数组为:

array (size=14)
  0 => 
    array (size=5)
      'CODIGO' => string '447' (length=3)
      'CONSECUTIVO_GRUPO' => string '1' (length=1)
      'ID_MODULO' => string '20' (length=2)
      'ID_GRUPO' => string '39' (length=2)
      'VALOR' => string '2023-09-05' (length=10)
  1 => 
    array (size=5)
      'CODIGO' => string '449' (length=3)
      'CONSECUTIVO_GRUPO' => string '1' (length=1)
      'ID_MODULO' => string '20' (length=2)
      'ID_GRUPO' => string '39' (length=2)
      'VALOR' => string '0' (length=1)
  2 => 
    array (size=5)
      'CODIGO' => string '450' (length=3)
      'CONSECUTIVO_GRUPO' => string '1' (length=1)
      'ID_MODULO' => string '20' (length=2)
      'ID_GRUPO' => string '39' (length=2)
      'VALOR' => string '5' (length=1)
  3 => 
    array (size=5)
      'CODIGO' => string '451' (length=3)
      'CONSECUTIVO_GRUPO' => string '1' (length=1)
      'ID_MODULO' => string '20' (length=2)
      'ID_GRUPO' => string '39' (length=2)
      'VALOR' => string '0' (length=1)
  4 => 
    array (size=5)
      'CODIGO' => string '452' (length=3)
      'CONSECUTIVO_GRUPO' => string '1' (length=1)
      'ID_MODULO' => string '20' (length=2)
      'ID_GRUPO' => string '39' (length=2)
      'VALOR' => string '2' (length=1)
  5 => 
    array (size=5)
      'CODIGO' => string '473' (length=3)
      'CONSECUTIVO_GRUPO' => string '1' (length=1)
      'ID_MODULO' => string '22' (length=2)
      'ID_GRUPO' => string '39' (length=2)
      'VALOR' => string 'M' (length=1)
  6 => 
    array (size=5)
      'CODIGO' => string '478' (length=3)
      'CONSECUTIVO_GRUPO' => string '1' (length=1)
      'ID_MODULO' => string '22' (length=2)
      'ID_GRUPO' => string '39' (length=2)
      'VALOR' => string '0' (length=1)
  7 => 
    array (size=5)
      'CODIGO' => string '475' (length=3)
      'CONSECUTIVO_GRUPO' => string '1' (length=1)
      'ID_MODULO' => string '22' (length=2)
      'ID_GRUPO' => string '39' (length=2)
      'VALOR' => string '0' (length=1)
  8 => 
    array (size=5)
      'CODIGO' => string '446' (length=3)
      'CONSECUTIVO_GRUPO' => string '1' (length=1)
      'ID_MODULO' => string '20' (length=2)
      'ID_GRUPO' => string '39' (length=2)
      'VALOR' => string 'H102' (length=4)
  9 => 
    array (size=5)
      'CODIGO' => string '472' (length=3)
      'CONSECUTIVO_GRUPO' => string '1' (length=1)
      'ID_MODULO' => string '22' (length=2)
      'ID_GRUPO' => string '39' (length=2)
      'VALOR' => string 'H102' (length=4)
  10 => 
    array (size=5)
      'CODIGO' => string '473' (length=3)
      'CONSECUTIVO_GRUPO' => string '2' (length=1)
      'ID_MODULO' => string '22' (length=2)
      'ID_GRUPO' => string '39' (length=2)
      'VALOR' => string 'M' (length=1)
  11 => 
    array (size=5)
      'CODIGO' => string '478' (length=3)
      'CONSECUTIVO_GRUPO' => string '2' (length=1)
      'ID_MODULO' => string '22' (length=2)
      'ID_GRUPO' => string '39' (length=2)
      'VALOR' => string '0' (length=1)
  12 => 
    array (size=5)
      'CODIGO' => string '475' (length=3)
      'CONSECUTIVO_GRUPO' => string '2' (length=1)
      'ID_MODULO' => string '22' (length=2)
      'ID_GRUPO' => string '39' (length=2)
      'VALOR' => string '0' (length=1)
  13 => 
    array (size=5)
      'CODIGO' => string '472' (length=3)
      'CONSECUTIVO_GRUPO' => string '2' (length=1)
      'ID_MODULO' => string '22' (length=2)
      'ID_GRUPO' => string '39' (length=2)
      'VALOR' => string 'H108' (length=4)

我需要按'CONSECUTIVO_GRUPO'分组,就像这样;

{
                            "CONSECUTIVO": "2",
                            "GRUPO": "1",
                            "RESPUESTAS": [
                                [
                                    "94",
                                    "76"
                                ],
                                [
                                    "96",
                                    "26.3"
                                ],
                                [
                                    "160",
                                    "170"
                                ],
                                [
                                    "163",
                                    "12.3"
                                ],
                                [
                                    "207",
                                    "130"
                                ],
                                [
                                    "208",
                                    "90"
                                ],
                                [
                                    "259",
                                    "2019-10-02"
                                ],
                                [
                                    "260",
                                    "2"
                                ],
                                [
                                    "261",
                                    "Z358"
                                ],
                                [
                                    "1209",
                                    "SP"
                                ],
                                [
                                    "1296",
                                    "5"
                                ],
                                [
                                    "1297",
                                    "0"
                                ]
                            ]
                        },
                        {
                            "CONSECUTIVO": "3",
                            "GRUPO": "1",
                            "RESPUESTAS": [
                                [
                                    "94",
                                    "76"
                                ],
                                [
                                    "96",
                                    "26.3"
                                ],
                                [
                                    "160",
                                    "170"
                                ],
                                [
                                    "163",
                                    "14.6"
                                ],
                                [
                                    "207",
                                    "130"
                                ],
                                [
                                    "208",
                                    "90"
                                ],
                                [
                                    "259",
                                    "2019-10-18"
                                ],
                                [
                                    "260",
                                    "2"
                                ],
                                [
                                    "261",
                                    "Z358"
                                ],
                                [
                                    "1209",
                                    "SP"
                                ],
                                [
                                    "1296",
                                    "10"
                                ],
                                [
                                    "1297",
                                    "0"
                                ]
                            ]
                        },

我还没有找到一个方法来做到这一点,有人可以帮助我,请,我有以下代码:

$result = [];
                        foreach ($rsp as $answer){                            
                            $result[] = array(
                                "CONSECUTIVO" => $answer['CONSECUTIVO_GRUPO'],
                                "GRUPO" => $answer['ID_GRUPO'],
                                "RESPUESTAS" => array(
                                    array(
                                        $answer['CODIGO'],
                                        $answer['ID_MODULO'],
                                        $answer['VALOR']
                                    )
                                )
                            );
                        }
des4xlb0

des4xlb01#

$result = [];

foreach ($rsp as $answer) {
    // Create a key for the data based on the grouping criteria
    $key = $answer['CONSECUTIVO_GRUPO'] . '-' . $answer['ID_GRUPO'];
    
    if( ! key_exists($key, $result) ) {
        // Add the full structure if it doesn't exist
        $result[$key] = [
            "CONSECUTIVO" => $answer['CONSECUTIVO_GRUPO'],
            "GRUPO" => $answer['ID_GRUPO'],
            "RESPUESTAS" => [
                [
                    $answer['CODIGO'],
                    $answer['ID_MODULO'],
                    $answer['VALOR']
                ]
            ]
        ];
    } else {
        // Otherwise just add the new items
        $result[$key]["RESPUESTAS"][] = [
            $answer['CODIGO'],
            $answer['ID_MODULO'],
            $answer['VALOR']
        ];
    }
}

// Strip the keys from the array so that the JSON generates correctly.
$result = array_values($result);

相关问题