Separate equal values in an array

Asked

Viewed 897 times

4

Well I have the following array:

$produtos2[] = array(
    "cod" => (int) 768,
    "nome" => "LOGITECH M535",
    "GRUPO" => "MOUSE"
);
$produtos2[] = array(
    "cod" => (int) 2334,
    "nome" => "MULTILASER DECT",
    "GRUPO" => "TECLADO"
);
$produtos2[] = array(
    "cod" => (int) 334,
    "nome" => "PANASONIC DECT",
    "GRUPO" => "MOUSE"
);
$produtos2[] = array(
    "cod" => (int) 3334,
    "nome" => "APPLE DECT",
    "GRUPO" => "TECLADO"
);
$produtos2[] = array(
    "cod" => (int) 234,
    "nome" => "SAMSUNG D499",
    "GRUPO" => "MOUSE"
);

To list the result of the array I do so:

// Navega pelos elementos do array
foreach ($produtos as $c) {

    echo $c['nome']."<br>";
}

The result is:

LOGITECH M535
MULTILASER DECT
PANASONIC DECT
APPLE DECT
SAMSUNG D499

Good what I need is to separate the products of have the same group and list them coming across by group, example:

MOUSE
    LOGITECH M535
    PANASONIC DECT
    SAMSUNG D499

TECLADO
    MULTILASER DECT
    APPLE DECT

I have no idea how to do this.

  • you can create other array (by group) and add data from the main array to them according to the group or turn into a json and resolve in the front-end

  • Well that’s a good idea, I can run the product array capturing the groups, but how to avoid group duplicity?

  • Is the group property dynamic? for example can be created one more at any time (daai complicates a little) or are fixed options (in this case just create array_teclado and array_mouse and go through the main array by passing the items, it would be interesting to remove these items from the main array, not to read more data unnecessarily)

  • It is dynamic, are various products with several different groups.

  • you have some table in the bank where these groups are registered or the user type in a input text group you want?

  • the user type in a text input.

Show 1 more comment

2 answers

4


Make a function to group:

function agrupar($array, $campoAgrupar) {
    $resultado = array();
    foreach($array as $valor) {
        $resultado[$valor[$campoAgrupar]][] = $valor;
    }
    return $resultado;
}

Grouping by "GRUPO" with the function indicated above generates you the following array::

array(2) {
  ["MOUSE"]=>
  array(3) {
    [0]=>
    array(3) {
      ["cod"]=>
      int(768)
      ["nome"]=>
      string(13) "LOGITECH M535"
      ["GRUPO"]=>
      string(5) "MOUSE"
    }
    [1]=>
    array(3) {
      ["cod"]=>
      int(334)
      ["nome"]=>
      string(14) "PANASONIC DECT"
      ["GRUPO"]=>
      string(5) "MOUSE"
    }
    [2]=>
    array(3) {
      ["cod"]=>
      int(234)
      ["nome"]=>
      string(12) "SAMSUNG D499"
      ["GRUPO"]=>
      string(5) "MOUSE"
    }
  }
  ["TECLADO"]=>
  array(2) {
    [0]=>
    array(3) {
      ["cod"]=>
      int(2334)
      ["nome"]=>
      string(15) "MULTILASER DECT"
      ["GRUPO"]=>
      string(7) "TECLADO"
    }
    [1]=>
    array(3) {
      ["cod"]=>
      int(3334)
      ["nome"]=>
      string(10) "APPLE DECT"
      ["GRUPO"]=>
      string(7) "TECLADO"
    }
  }
}

See in Ideone the result of the grouping

To show it the way you want it just use two foreach:

$produtosPorGrupo = agrupar($produtos2,"GRUPO");

foreach ($produtosPorGrupo as $nomeGrupo => $grupo){
    echo $nomeGrupo . PHP_EOL;
    foreach ($grupo as $prod){
        echo "\t" . $prod['nome']. PHP_EOL;
    }
}

See also in Ideone the result already with the 2 foreach to show

Final exit:

MOUSE
    LOGITECH M535
    PANASONIC DECT
    SAMSUNG D499
TECLADO
    MULTILASER DECT
    APPLE DECT

Edit:

To sort by the grouped array, just use one of the existing php functions that sorts by the key:

  • ksort - increasing order by key
  • krsort - decreasing ordering by key

Soon I’d be like this:

$produtosPorGrupo = agrupar($produtos2,"GRUPO");    
ksort($produtosPorGrupo);

//resto do código para mostrar
  • 1

    Very good, it’s top.

  • @Hugoborges I edited the answer to contemplate ordination

4

Use array_column() to get all available groups, then just do a foreach by passing as key the current item (group) and the value the full item.

$grupos = array_column($produtos2, 'GRUPO');
$novo = array();
foreach ($produtos2 as $item){
    $novo[$item['GRUPO']][] = $item;
}

Example - idoene

  • Vlw, good solution.

Browser other questions tagged

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