Grouping Lines from an Array - PHP

Asked

Viewed 84 times

-1

I have an SQL query that returns a set of values:

cod_Vd  nome_Vend   cod_eqp nome_equipe
  46    46 - EVANDRO    1   EQUIPE -01
  87    87 - ALAN       1   EQUIPE -01
  44    44 - ANA        2   EQUIPE - 02
  84    84 - JORGEVAL   2   EQUIPE - 02

I wonder if there is any function or logic in PHP that organizes a array with these data as follows:

1   EQUIPE -01
46  46 - EVANDRO
87  87 - ALAN

2   EQUIPE - 02
44  44 - ANA 
84  84 - JORGEVAL

Here’s a short example of the code I tried:

$arrayVendedores = array_unique($arrayVendedoresConsultaSQL);

foreach($arrayVendedores as $value) {

      echo($value["equipeVendedor"]);

}

foreach($arrayVendedores as $valueDois) {

    if($value["equipeVendedor"]==$valueDois["equipeVendedor"]) {

         echo($valueDois["nome_vendedor"]);

    }

}

Upshot:

1   EQUIPE - 01
46  46 - EVANDRO
87  87 - ALAN
1   EQUIPE - 01
46  46 - EVANDRO
87  87 - ALAN
2   EQUIPE - 02
44  44 - ANA 
84  84 - JORGEVAL
2   EQUIPE - 02
44  44 - ANA 
84  84 - JORGEVAL

1 answer

0

You will have to organize the array yourself, creating complete logic. The easiest and most cost-effective way for the bank would be to slowly search. Example: Select from team 01 with order by of number and pick array. Then select from team 02 equal team 01, take the array and merge with the team 01 array and so on.

Or you can do bring it all at once with team order by create logic within the foreach. Example:

$arrayOrganizado = new array();
foreach ($resultados as $row){
  if (!isset($arrayOrganizado[$value["equipeVendedor"]]) {
     $arrayOrganizado[$value["equipeVendedor"]][] = $valor;
  }

}

Use indexes to organize data.

Browser other questions tagged

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