Group values from an array in PHP

Asked

Viewed 5,927 times

6

I have a grid form that returns the following values within an Array:

array (size=3)
  0 => 
    array (size=5)
      'pei_seq' => int 0
      'prg_cod_barra' => string '7899619704729' (length=13)
      'pei_prg_cod' => string '483' (length=3)
      'pei_valor' => string '42.00' (length=5)
      'pei_quantidade' => string '1' (length=1)
  1 => 
    array (size=5)
      'pei_seq' => int 1
      'prg_cod_barra' => string '7899619704729' (length=13)
      'pei_prg_cod' => string '483' (length=3)
      'pei_valor' => string '42.00' (length=5)
      'pei_quantidade' => string '1' (length=1)
  2 => 
    array (size=5)
      'pei_seq' => int 2
      'prg_cod_barra' => string '7899619704705' (length=13)
      'pei_prg_cod' => string '481' (length=3)
      'pei_valor' => string '31.00' (length=5)
      'pei_quantidade' => string '1' (length=1)

What I’d like to know is if there’s any function to unite the values of pei_quantidade where the values of prg_cod_barra are the same. In this case, I wanted the array to return:

array (size=2)
  0 => 
    array (size=5)
      'pei_seq' => int 0
      'prg_cod_barra' => string '7899619704729' (length=13)
      'pei_prg_cod' => string '483' (length=3)
      'pei_valor' => string '42.00' (length=5)
      'pei_quantidade' => string '2' (length=1)
  1 => 
    array (size=5)
      'pei_seq' => int 2
      'prg_cod_barra' => string '7899619704705' (length=13)
      'pei_prg_cod' => string '481' (length=3)
      'pei_valor' => string '31.00' (length=5)
      'pei_quantidade' => string '1' (length=1)

There is such a function?

  • If the data is coming repeated it is easier to give a GROUP BY at the time of query mysql

  • But this data does not come from Mysql, are informed in the form

  • could not use prg_cod_barra as the main key?

3 answers

3


I suggest you iterate this array and make a new array during iteration. At each accurate iteration check for an equal value in the new array.

Suggestion:

$nova = [];
foreach ($original as $arr) { // itera a array original
    $prg_cod_barra = $arr['prg_cod_barra'];
    $existe = false;

    foreach ($nova as &$subArr) { // iterar a array nova à procura de igual
       // se houver igualdade 
       if ($prg_cod_barra == $subArr['prg_cod_barra']){
          $valorAntigo = intval($subArr['pei_quantidade'], 10);
          $novoValor = intval($arr['pei_quantidade'], 10);
          $existe = strval($valorAntigo + $novoValor); // inserir o novo numero
          $subArr['pei_quantidade'] = $existe;
       }
    }
    if (!$existe) $nova[] = $arr;
}

Example: https://ideone.com/drcgH5

I assume you want to keep string in the field pei_quantidade hence I used the intval to know the string value and then stringval to convert the sum of the values into a string.

  • I used your code and got an empty array as a result, you know what it could be?

  • @Lucaspadilha had an error, correct, must be $nova[] = $arr; in the else. I didn’t test with code but I think now the logic is right.

  • I had already performed this haha test. Still returning an empty array.

  • As far as I can tell, the code is not entering the foreach ($nova as $subArr)

  • @Lucaspadilha gave the name of $original to your initial array?

  • yes, it is adapted to my code, but it is not entering the routine of the second foreach. I think the error is happening because the variable $nova does not receive any value, this is returning array (size=0)
 empty.

  • @Lucaspadilha after all had one more error in my code. Now I had time to fix and joined example.

  • I have solved the issue in another way, but yours works perfectly too. Thanks for the help.

Show 3 more comments

1

I performed some tests and managed to solve my problem. Here is the code I used:

$nova = [];
foreach($origial as $a) {
  if (!isset($nova[$a['prg_cod_barra']])) {
    $nova[$a['prg_cod_barra']] = $a;
  } else {
    if ($nova[$a['prg_cod_barra']]['prg_cod_barra'] == $a['prg_cod_barra']) {
      $nova[$a['prg_cod_barra']]['pei_quantidade'] = strval($a['pei_quantidade'] + $nova[$a['prg_cod_barra']]['pei_quantidade']); 
    } 
  }
}

When performing a var_dump() in the variable $nova the return will be:

array (size=2)
  '7899619704712' => 
    array (size=5)
      'pei_seq' => int 0
      'prg_cod_barra' => string '7899619704712' (length=13)
      'pei_prg_cod' => string '482' (length=3)
      'pei_valor' => string '0.04' (length=4)
      'pei_quantidade' => string '2' (length=1)
  '7899619704668' => 
    array (size=5)
      'pei_seq' => int 2
      'prg_cod_barra' => string '7899619704668' (length=13)
      'pei_prg_cod' => string '477' (length=3)
      'pei_valor' => string '51.00' (length=5)
      'pei_quantidade' => string '1' (length=1)
  • What remains to be resolved?

  • 1

    You would only need to put the variable pei_seq in sequence, but this escapes the topic of the question and is not as important as what has already been solved.

  • @Lucaspadilha see if this helps http://php.net/manual/en/function.array-multisort.php or any of these http://php.net/manual/en/array.sorting.php

  • Yeah, I just put a counter inside the foreach and assigned the value of that counter to the field pei_seq. But thank you in the same way.

0

I found a code that might help you

$result = array();
foreach ($arr as $data) {
  $id = $data['prg_cod_barra'];
  if (isset($result[$id])) {
     $result[$id][] = $data;
  } else {
     $result[$id] = array($data);
  }
}

Source reply

  • It is not exactly what I was looking for, but it has already given me a light, I will try to adapt it to the project. Thank you

Browser other questions tagged

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