Group values from an array in PHP

6

I have a grid-shaped form that returns the following values inside 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 would like to know is if there is any function to join the values of pei_quantidade whenever the values of prg_cod_barra are equal. In 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)

Is there such a function?

    
asked by anonymous 06.03.2015 / 13:50

3 answers

3

I suggest you iterate this array and make a new array during iteration. In each iteration you need to check if there is already 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: link

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

06.03.2015 / 14:07
1

I ran some tests and solved 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() on 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)
    
06.03.2015 / 16:02
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 response

    
06.03.2015 / 13:56