Search code examples
phparraysmultidimensional-arraygroupingsub-array

How to perform grouping in php


I have the following array,in which, I have an item 'CONSECUTIVO_GRUPO', which I want to take as a reference to create a group of 'RESPONSES' in an array of three positions, CODIGO, ID_MODULO, VALOR. The initial array is:

    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)

I need grouping by 'CONSECUTIVO_GRUPO', like that;

{
                            "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"
                                ]
                            ]
                        },

I haven't found a way to do it, could someone help me please, I have the following code:

$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']
                                    )
                                )
                            );
                        }

Solution

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