Group Data from a PHP Array

0

I want to group the data with the same code as this array, I've tried several ways, but so far nothing. Anyone have an idea?

array (size=5)
      0 => 
        array (size=2)
          'CODIGO' => string '231' (length=3)
          'NOME' => string 'Mary' (length=22)
      1 => 
        array (size=2)
          'CODIGO' => string '188' (length=3)
          'NOME' => string 'Bernardino' (length=16)
      2 => 
        array (size=2)
          'CODIGO' => string '188' (length=3)
          'NOME' => string 'Carlos' (length=19)
      3 => 
        array (size=2)
          'CODIGO' => string '93' (length=2)
          'NOME' => string 'Alessandra' (length=17)
      4 => 
        array (size=2)
          'CODIGO' => string '93' (length=2)
          'NOME' => string 'Aparecida' (length=9)
      5 => 
        array (size=2)
          'CODIGO' => string '93' (length=2)
          'NOME' => string 'Raquel' (length=12)

Thanks in advance!

    
asked by anonymous 23.06.2016 / 22:33

3 answers

0

I got this way, my array is in $ recordCustomers.

$temp = array();

    foreach($recordCustomers as $arg)
    {
        $temp[$arg['CODIGO']][] = $arg['NOME'];
    }

    $output = array();

    foreach($temp as $id => $name)
    {
        $output[] = array(
                'CODIGO' => $id,
                'NOME' => $name
        );
    }


    var_dump($output);
    
23.06.2016 / 22:52
1

Something like this?

$dados[] = array('CODIGO' => 231, 'NOME' => 'Mary');
$dados[] = array('CODIGO' => 188, 'NOME' => 'Bernardino');
$dados[] = array('CODIGO' => 188, 'NOME' => 'Carlos');
$dados[] = array('CODIGO' => 93, 'NOME' => 'Alessandra');
$dados[] = array('CODIGO' => 93, 'NOME' => 'Aparecida');
$dados[] = array('CODIGO' => 93, 'NOME' => 'Raquel');

$dadosAgrupados = array();
foreach ($dados as $dado) {
    $dadosAgrupados[$dado['CODIGO']][] = $dado['NOME'];
}
    
23.06.2016 / 22:42
1

Do you want to generate a dynamic array in php? try this

<?php

$arr = array(
        array('CODIGO'=>'231','NOME'=>'Mary'),
        array('CODIGO'=>'188','NOME'=>'Bernardino'),
        array('CODIGO'=>'188','NOME'=>'Carlos'),
        array('CODIGO'=>'93','NOME'=>'Alessandra'),
        array('CODIGO'=>'93','NOME'=>'Aparecida'),
        array('CODIGO'=>'93','NOME'=>'Raquel')
      );
 print_r($arr);
?>
    
23.06.2016 / 22:39