0

I have got an array like this:

array (
    0 => 
    array (
      'total_price' => '19.120000',
      'total_percent' => '0.20',
      'vat_code' => 22,
      'discount_percent' => '3.82',
    ),
    1 => 
    array (
      'total_price' => '58.000000',
      'total_percent' => '0.60',
      'vat_code' => 22,
      'discount_percent' => '11.60',
    ),
    2 => 
    array (
      'total_price' => '20.000000',
      'total_percent' => '0.21',
      'vat_code' => 4,
      'discount_percent' => '4.00',
    ),
  )

I'm trying to search values using 'vat_code' as key, and sum total_price, total_percent and discount_percent values if I find the same 'vat_code' value.

In other words, I wold like to obtain a result like this:

array (
    0 => 
    array (
      'total_price' => '77.120000', // result of 58 + 19.12
      'total_percent' => '0.80', // result of 0.60 + 0.20
      'vat_code' => 22,
      'discount_percent' => '15.42', // result of 11.60 + 3.82
    ),
    1 => 
    array (
      'total_price' => '20.000000',
      'total_percent' => '0.21',
      'vat_code' => 4,
      'discount_percent' => '4.00',
    ),
  )

edit: here below the code I tried.

 $rate_quantity_per_price = array();              
 $vat_code = intval($product_detail['tax_rate']);
 $total_products = $order->total_products; 
 $discount_neat = $order->total_discounts_tax_excl; // 
 $quantity_row = $product_detail['product_quantity'];
 $quantity_per_price = $product_detail['total_price_tax_excl'];

$row_total_price = $quantity_per_price / $total_products; 
$item_row_total_percent = number_format($row_total_price, 2, '.', '');

$discount_splitted = $discount_neat * $row_total_price;
$row_discount_splitted = number_format($discount_splitted, 2, '.', '');


$row_product[] = array('total_price' => $quantity_per_price,
                       'total_percent' => $item_row_total_percent,
                       'vat_code' => $vat_code,
                       'discount_percent' => $row_discount_splitted);



if(!array_search($vat_code, $row_product)){
      $rate_quantity_per_price[$vat_code] = $quantity_per_price;
      $rate_total_price[$vat_code] = $item_row_total_percent;
      $rate_discount_row[$vat_code] = $row_discount_splitted;

 } else {
      $rate_quantity_per_price[$vat_code] += $quantity_per_price;
      $rate_total_price[$vat_code] += $item_row_total_percent;
      $rate_discount_row[$vat_code] += $row_discount_splitted;
 }   

Thanks for all your support :-)

2
  • 1
    Suggestion - start writing code. Commented Oct 22, 2019 at 13:20
  • Show us what did you try? Commented Oct 22, 2019 at 13:21

3 Answers 3

1

iterate using foreach loop

foreach($a as $v){
 isset($group[$v['vat_code']]) ?
  ($group[$v['vat_code']]['total_price'] += $v['total_price'] AND 
    $group[$v['vat_code']]['total_percent'] += $v['total_percent'] AND
    $group[$v['vat_code']]['discount_percent'] += $v['discount_percent']
  )
  :
 ($group[$v['vat_code']] = $v);
}
print_r(array_values($group));

Working example :- https://3v4l.org/Fp1hu

Sign up to request clarification or add additional context in comments.

Comments

0

You have a single dimens array as input and output, therefore you will need at least one loop. Which loop type is up to you. You will need to initialize the output array before your loop starts to an empty array. Inside your loop you will need a conditional that tests the output array for the existence of the vat key of the current iterated record in the input array. That is your loop will need to iterate the input array. If the key is in the output array, then update that record's data in the output array; otherwise if it's not in the output array, add the record to the output array anew.

Pseudo code:

init output array
for records in input array:
     if input record's vat is in output array then:
          update output record
     else
          add record to output array
end for

Comments

0

At last I found the solution.

Here below the code:

$rate_products_array = array(); // output array

      foreach($row_product as $n => $value) {
          $vat_value = $value['vat_code'];

          if(!array_key_exists($vat_value, $rate_products_array)) {
                   $rate_products_array[$vat_value]['total_price'] = $value['total_price'];
                   $rate_products_array[$vat_value]['total_percent'] = $value['total_percent'];
                   $rate_products_array[$vat_value]['discount_percent'] = $value['discount_percent'];

          } else {
                    $rate_products_array[$vat_value]['total_price'] += $value['total_price'] ;
                    $rate_products_array[$vat_value]['total_percent'] += $value['total_percent'] ;
                    $rate_products_array[$vat_value]['discount_percent'] += $value['discount_percent'];
          }
               //$this->trace('rate products array :', $rate_products_array);

       } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.