Grouping data from a PHP Array

Asked

Viewed 476 times

0

I want to group the data with the same code of this array, I have tried several ways, but so far nothing. Someone has 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)

From now on, thank you!

  • 5

    Your question is not very clear. What data do you want to group? Here are some tips on how to ask a slightly more complete question, to increase the chance of an answer that helps: [Ask] and [Help]. Then you can [Dit] the post and add details.

3 answers

1

Do you want to generate a dientic array for this 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);
?>
  • Would not be 'NOME' => 'Aparecida' for example?

  • @Ivcs updated thank you

1

Something like that?

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

0


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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.