Loop and merge if there are the same key - php

Array ( [0] => Array ( [field_airline] => 18 [title] => FGGH [field_route_location] => Hongkong [field_time] => 01:10 )
[1] => Array ( [field_airline] => 19 [title] => DSSA [field_route_location] => Kuala Lumpur [field_time] => 01:10 ),
[2] => Array ( [field_airline] => 19 [title] => ASAS [field_route_location] => Kuala Lumpur [field_time] => 01:10 )
)
The merge result I want
Array ( [0] => Array ( [field_airline] => 18 [title] => FGGH [field_route_location] => Hongkong [field_time] => 01:10 ),
[1] => Array ( [field_airline] => 19 [title] => array(DSSA,ASAS) [field_route_location] => Kuala Lumpur [field_time] => 01:10)
)
I AM TRYING
<?php
$a= Array ( 0 => Array ( 'field_airline' => 18 ,
'title' => 'FGGH',
'field_route_location' => 'Hongkong',
'field_time' => '01:10'
),
1 => Array ( 'field_airline' => 19 ,
'title' => 'DSSA',
'field_route_location' => 'Kuala Lumpur',
'field_time' => '01:10'
),
2 => Array ( 'field_airline' => 19,
'title' => 'ASAS',
'field_route_location' => 'Kuala Lumpur',
'field_time' => '01:10'
)
);
$b = array();
foreach ($a as $key=>$value){
$b[$value['field_route_location']][] = $value;
}
echo '<pre>';
print_r($b);
echo '</pre>';
?>

function sortByLocation($a, $b)
{
return strcmp($a["field_route_location"], $b["field_route_location"]);
}
$a = Array(
0 => Array('field_airline' => 18,
'title' => 'FGGH',
'field_route_location' => 'Hongkong',
'field_time' => '01:10'
),
1 => Array('field_airline' => 19,
'title' => 'DSSA',
'field_route_location' => 'Kuala Lumpur',
'field_time' => '01:10'
),
2 => Array('field_airline' => 19,
'title' => 'ASAS',
'field_route_location' => 'Kuala Lumpur',
'field_time' => '01:10'
)
);
usort($a, 'sortByLocation'); // sorting array by location
$b = array();
foreach ($a as $key => $value)
{
$title = array();
if (!isset($b[$value['field_route_location']]))
$b[$value['field_route_location']] = $value;
else
{
if (!is_array($b[$value['field_route_location']]['title']))
array_push($title, $b[$value['field_route_location']]['title']);
else
$title = $b[$value['field_route_location']]['title'];
array_push($title, $a[$key]['title']);
$b[$value['field_route_location']]['title'] = $title;
}
}
echo '<pre>';
print_r($b);
echo '</pre>';
Output
Array
(
[Hongkong] => Array
(
[field_airline] => 18
[title] => FGGH
[field_route_location] => Hongkong
[field_time] => 01:10
)
[Kuala Lumpur] => Array
(
[field_airline] => 19
[title] => Array
(
[0] => ASAS
[1] => DSSA
)
[field_route_location] => Kuala Lumpur
[field_time] => 01:10
)
)

foreach ($a as $ka=>&$va)
{
foreach ($a as $kb=>$vb)
{
if ($va['field_airline'] == $vb['field_airline'] && $ka != $kb)
{
if (is_array($va['title']))
{
$va['title'][] = $vb['title'];
}
else
{
$va['title'] = array($va['title'], $vb['title']);
}
// repeat if/else with the other fields if needed
unset($a[$kb]);
}
}
}

Concept:
Create a blank results array. Loop through the given data. For every item, if it does not exist in results array, push it. If it does exist, push the item's title to the results array for that field_airline element.
Code:
$array_result = array();
if ( ! empty($array)){
foreach($array as $item){
// item already exists in results
if (isset($array_result[$item['field_airline']])){
// prepare title
$title = $array_result[$item['field_airline']]['title'];
$title[] = $item['title']; // push into existing array
// replace array of titles
$array_result[$item['field_airline']]['title'] = $title;
}
// encountering for first time
else{
$array_result[$item['field_airline']] = array(
'field_airline' => $item['field_airline'],
'title' => array($item['title']),
'field_route_location' => $item['field_route_location'],
'field_time' => $item['field_time'],
);
}
}
}
$array_result = array_values($array_result); // reset the keys in results
Test Code:
$array = array(
array(
'field_airline' => 18,
'title' => 'FGGH',
'field_route_location' => 'Hongkong',
'field_time' => '01:10',
),
array(
'field_airline' => 19,
'title' => 'DSSA',
'field_route_location' => 'Kuala Lumpur',
'field_time' => '01:10',
),
array(
'field_airline' => 19,
'title' => 'ASAS',
'field_route_location' => 'Kuala Lumpur',
'field_time' => '01:10',
),
);

Try this.
<?php
$arr=array('0' => array('field_airline' => 18,
'title' => 'FGGH',
'field_route_location' => 'Hongkong',
'field_time' => '01:10'),
'1' => array('field_airline' => 19,
'title' => 'DSSA',
'field_route_location' => 'Kuala Lumpur',
'field_time' => '01:10'),
'2' => array('field_airline' => 19,
'title' => 'ASAS',
'field_route_location' => 'Test',
'field_time' => '01:10')
);
$final=array();
$uniqkeys=array(); // to check for unique field_airline
foreach($arr as $key => $subarr)
{
if(!in_array($subarr['field_airline'],$uniqkeys))
{
$uniqkeys[]=$subarr['field_airline'];
$final[$subarr['field_airline']]= $subarr;
}
else
{
// Check if title is not same, add title to array
if($final[$subarr['field_airline']]['title'] != $subarr['title'])
{
$final[$subarr['field_airline']]['title']=array(
$final[$subarr['field_airline']]['title'], $subarr['title']);
}
//Check for location
if($final[$subarr['field_airline']]['field_route_location'] != $subarr['field_route_location'])
{
$final[$subarr['field_airline']]['field_route_location']=array(
$final[$subarr['field_airline']]['field_route_location'], $subarr['field_route_location']);
}
// Check for filed time
if($final[$subarr['field_airline']]['field_time'] != $subarr['field_time'])
{
$final[$subarr['field_airline']]['field_time']=array(
$final[$subarr['field_airline']]['field_time'], $subarr['field_time']);
}
}
}
print_r($final);
?>
Check the working example here(http://codepad.org/UkOZq9gy).

Related

PHP add array values with same date

If view is less than one I would like to output to remain as it is but if view > 0 I would like to add all the values that share the same date and output it like:-
Array
(
[0] => Array
(
[date] => 09-04-2018
[value] => 30
)
[1] => Array
(
[date] => 10-04-2018
[value] => 32
)
[2] => Array
(
[date] => 11-04-2018
[value] => 34
)
[3] => Array
(
[date] => 12-04-2018
[value] => 36
)
[4] => Array
(
[date] => 13-04-2018
[value] => 39
)
)
I am using the following code which works for view = 0 but does not add the correct values when view = 1:-
$view = 1;
$a = array(
array('name' => 'Days','date' => '09-04-2018','value' => '10'),
array('name' => 'Nights','date' => '09-04-2018','value' => '20'),
array('name' => 'Days','date' => '10-04-2018','value' => '11'),
array('name' => 'Nights','date' => '10-04-2018','value' => '21'),
array('name' => 'Days','date' => '11-04-2018','value' => '12'),
array('name' => 'Nights','date' => '11-04-2018','value' => '22'),
array('name' => 'Days','date' => '12-04-2018','value' => '13'),
array('name' => 'Nights','date' => '12-04-2018','value' => '23'),
array('name' => 'Days','date' => '13-04-2018','value' => '14'),
array('name' => 'Days','date' => '13-04-2018','value' => '24'),
array('name' => 'Days','date' => '13-04-2018','value' => '1')
);
$i = 0;
foreach ($a as $b)
{
$date = $b['date'];
$value = $b['value'];
$key = $i;
if ($view > 0)
{
if ( $key = array_search($date, array_column($c, 'date')) > 0 )
{
$value = $c[$key]['value'] + $value;
}
else
{
$key = $i;
}
}
$c[$key]['name'] = 'Combined';
$c[$key]['date'] = $date;
$c[$key]['value'] = $value;
$i++;
}
echo "<pre>";
print_r($c);
echo "</pre>";
can anyone tell me where I am going wrong?
The problem is that you use $key = $i in the case of $view > 0, but, $i could be greater than the current size of $c. Then, the comparison with the value of array_keys must be a strict comparison the check if the key if different or greater that 0, or false (not found). finally, you need to create an empty array $c to start to avoid a warning in array_column.
$view = 1;
$a = array(
array('name' => 'Days','date' => '09-04-2018','value' => '10'),
array('name' => 'Nights','date' => '09-04-2018','value' => '20'),
array('name' => 'Days','date' => '10-04-2018','value' => '11'),
array('name' => 'Nights','date' => '10-04-2018','value' => '21'),
array('name' => 'Days','date' => '11-04-2018','value' => '12'),
array('name' => 'Nights','date' => '11-04-2018','value' => '22'),
array('name' => 'Days','date' => '12-04-2018','value' => '13'),
array('name' => 'Nights','date' => '12-04-2018','value' => '23'),
array('name' => 'Days','date' => '13-04-2018','value' => '14'),
array('name' => 'Days','date' => '13-04-2018','value' => '24'),
array('name' => 'Days','date' => '13-04-2018','value' => '1')
);
$c =[];
foreach ($a as $i => $b)
{
$date = $b['date'];
$value = $b['value'];
$key = $i;
if ($view > 0)
{
$key = array_search($date, array_column($c, 'date'));
if ($key !== false)
{
$value = $c[$key]['value'] + $value;
}
else
{
$key = count($c); // Create a new index here instead of $i
}
}
else
{
$key = $i;
}
$c[$key]['name'] = 'Combined';
$c[$key]['date'] = $date;
$c[$key]['value'] = $value;
}
print_r($c);
outputs:
Array
(
[0] => Array
(
[name] => Combined
[date] => 09-04-2018
[value] => 30
)
[1] => Array
(
[name] => Combined
[date] => 10-04-2018
[value] => 32
)
[2] => Array
(
[name] => Combined
[date] => 11-04-2018
[value] => 34
)
[3] => Array
(
[name] => Combined
[date] => 12-04-2018
[value] => 36
)
[4] => Array
(
[name] => Combined
[date] => 13-04-2018
[value] => 39
)
)

PHP array merge based on key & value

I have one array and try to change some key and value for example if sku are same than i need to merge image. Below array i have
Array
(
[0] => Array
(
[sku] => h-eldora
[name] => H ELDORA
[image] => s/files/1/1282/4221/products/h_eldora_01.jpg?v=1476667054
)
[1] => Array
(
[sku] => h-eldora
[name] =>
[image] => s/files/1/1282/4221/products/h_eldora_02.jpg?v=1475116221
)
[2] => Array
(
[sku] => h-eldora
[name] =>
[image] => s/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598
)
[3] => Array
(
[sku] => hl-dracy
[name] => HL DRACY
[image] => s/files/1/1282/4221/products/h_dracy_01.jpg?v=1475115222
)
[4] => Array
(
[sku] => hl-dracy
[name] =>
[image] => s/files/1/1282/4221/products/h_dracy_02.jpg?v=1475115223
)
[5] => Array
(
[sku] => hl-dracy
[name] =>
[image] =>s/files/1/1282/4221/products/20100707164858197_1_633237aa-36ec-441b-a074-03844f6a0858.jpg?v=1475719793
)
)
I need to merge array like this
Array
(
[0] => Array
(
[sku] => h-eldora
[name] =>
[image1] => s/files/1/1282/4221/products/h_eldora_02.jpg?v=1475116221
[image2] => s/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598
[image3] => s/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598
)
[1] => Array
(
[sku] => hl-dracy
[name] => HL DRACY
[image1] => s/files/1/1282/4221/products/h_dracy_01.jpg?v=1475115222
[image2] => s/files/1/1282/4221/products/h_dracy_02.jpg?v=1475115223
[image3] => s/files/1/1282/4221/products/20100707164858197_1_633237aa-36ec-441b-a074-03844f6a0858.jpg?v=1475719793
)
)
If any php function is there than please let me know or any code suggestion
Using simple PHP:
<?php
$arr1 = array(
0 => array(
'sku' => 'h-eldora',
'name' => 'H ELDORA',
'image' => 's/files/1/1282/4221/products/h_eldora_01.jpg?v=1476667054'
),
1 => array(
'sku' => 'h-eldora',
'name' => '',
'image' => 's/files/1/1282/4221/products/h_eldora_02.jpg?v=1475116221'
),
2 => array(
'sku' => 'h-eldora',
'name' => '',
'image' => 's/files/1/1282/4221/products/20100707164858197_1_88da6866-701a-42b9-b523-5e454cbcce70.jpg?v=1475717598'
),
3 => array(
'sku' => 'hl-dracy',
'name' => 'HL DRACY',
'image' => 's/files/1/1282/4221/products/h_dracy_01.jpg?v=1475115222'
),
4 => array(
'sku' => 'hl-dracy',
'name' => 'H ELDORA',
'image' => 's/files/1/1282/4221/products/h_dracy_02.jpg?v=1475115223'
),
5 => array(
'sku' => 'hl-dracy',
'name' => 'H ELDORA',
'image' => 's/files/1/1282/4221/products/20100707164858197_1_633237aa-36ec-441b-a074-03844f6a0858.jpg?v=1475719793'
)
);
$newArr = $imgIndex = array();
foreach($arr1 as $a){
if( !array_key_exists($a['sku'],$newArr) ){
$newArr[$a['sku']] = array(
'sku' => $a['sku'],
'name' => $a['name'],
'image1' => $a['image']
);
$imgFound[$a['sku']] = 1;
}else{
$imgFound[$a['sku']]++;
$newArr[$a['sku']]['image'.$imgFound[$a['sku']]] = $a['image'];
}
}
unset($imgFound);
echo '<pre>'; print_r($newArr); echo '</pre>';
?>
This could work for you:
$data = // your input array
$uniqueSKUs = Array();
$newArray = Array();
$currentIndex = -1;
foreach ($data as $item) {
if (!in_array($item['sku'], $uniqueSKUs)) {
$currentIndex++;
$uniqueSKUs[] = $item['sku'];
$newArray[$currentIndex] = Array(
'sku' => $item['sku'],
'name' => $item['name']
);
}
$newArray[$currentIndex]['images'][] = $item['image'];
}
echo "<pre>";
var_dump($newArray);
echo "</pre>";

PHP multiple duplicate values in multidimensional array

I am trying to update the below array to set "duplicate" => true when both "name" and "date" are the same. In the below example 'array[1][duplicate]=>true' as both
array[0] & array[1] have the same "name"=john & "date"=2015-7-24
Array
(
[0] => Array
(
[id] => 1
[name] => john
[date] => 2015-07-24
[duplicate] => false
)
[1] => Array
(
[id] => 1
[name] => john
[date] => 2015-07-24
[duplicate] => false
)
[2] => Array
(
[id] => 1
[name] => jane
[date] => 2015-07-24
[duplicate] => false
)
[3] => Array
(
[id] => 1
[name] => notJaneORJohn
[date] => 2015-07-24
[duplicate] => false
)
[4] => Array
(
[id] => 1
[name] => jane
[date] => 2099-07-24
[duplicate] => false
)
)
Try this,
$array = Array
(
0 => Array
(
'id' => '1',
'name' => 'john',
'date' => '2015-07-24',
'duplicate' => 'false',
),
1 => Array
(
'id' => 1,
'name' => 'john',
'date' => '2015-07-24',
'duplicate' => 'false'
),
2 => Array
(
'id' => 1,
'name' => 'jane',
'date' => '2015-07-24',
'duplicate' => 'false'
),
3 => Array
(
'id' => 1,
'name' => 'notJaneORJohn',
'date' => '2015-07-24',
'duplicate' => 'false'
),
4 => Array
(
'id' => 1,
'name' => 'jane',
'date' => '2099-07-24',
'duplicate' => 'false'
)
);
foreach ($array as $key => $value) {
for ($i = $key + 1 ; $i < sizeof($array); $i++) {
if ($value['name'] === $array[$i]['name'] && $value['date'] === $array[$i]['date']) {
$array[$key]['duplicate'] = 'TRUE';
$array[$i]['duplicate'] = 'TRUE';
}
}
}
This would work:
$Arr = Array(
['id'=>1, 'name'=>'john', 'date'=>'2015-07-24', 'duplicate'=>0],
['id'=>1, 'name'=>'john', 'date'=>'2015-07-24', 'duplicate'=>0],
['id'=>1, 'name'=>'Jane', 'date'=>'2015-07-24', 'duplicate'=>0]
);
foreach($Arr as $i1 => $v1){
$Str1 = $v1['name'].$v1['date'];
foreach($Arr as $i2 => $v2){
if( $i1 !== $i2 && $Str1 === $v2['name'].$v2['date'] ){
$Arr[$i1]['duplicate'] = 1;
}
}
}
echo '<pre>',print_r($Arr),'</pre>'; die();
... outputs:
Array(
[0] => Array
(
[id] => 1
[name] => john
[date] => 2015-07-24
[duplicate] => 1
)
[1] => Array
(
[id] => 1
[name] => john
[date] => 2015-07-24
[duplicate] => 1
)
[2] => Array
(
[id] => 1
[name] => Jane
[date] => 2015-07-24
[duplicate] => 0
)
)
Have a look at this shortcut method ;)
<?php $testarr = array(
array("id" => 1,"name" => "john","date" => "2015-07-24","duplicate" => "false"),
array("id" => 1,"name" => "john","date" => "2015-07-24","duplicate" => "false"),
array("id" => 1,"name" => "jane","date" => "2015-07-24","duplicate" => "false"),
array("id" => 1,"name" => "notJaneORJohn","date" => "2015-07-24","duplicate" => "false"),
array("id" => 1,"name" => "jane","date" => "2099-07-24","duplicate" => "false")
);
$tempArray = array();
function checkDuplicate(&$arr) {
global $tempArray;
if (count($tempArray) > 0 && in_array($arr['name'], $tempArray) && in_array($arr['date'], $tempArray)) {
$arr['duplicate'] = "true";
} else {
$tempArray[] = $arr['name'];
$tempArray[] = $arr['date'];
}
}
array_walk($testarr, 'checkDuplicate');
print_r($testarr);

How to substract values by date from array

I have the following array and i need to make the difference of values for each interval
Array
(
[0] => Array
(
[Mark] => 0
[ID] => 6236
[Date] => 2015-03-16 12:04:21
[Value] => 2
)
[1] => Array
(
[Mark] => 0
[ID] => 6236
[Date] => 2015-04-16 12:04:21
[Value] => 4
)
[2] => Array
(
[Mark] => 0
[ID] => 6236
[Date] => 2015-05-16 12:04:21
[Value] => 9
)
[3] => Array
(
[Mark] => 0
[ID] => 6236
[Date] => 2015-06-16 12:04:21
[Value] => 15
)
)
and I need to make the difference for each interval.
something like
[Date] => 2015-04-16 12:04:21 => [Value][1] - [Value][0] = 2
[Date] => 2015-05-16 12:04:21 => [Value][2] - [Value][1] = 5
[Date] => 2015-06-16 12:04:21 => [Value][3] - [Value][2] = 6
How can I do that?
You need to loop through this array to create new one with those differences, for example:
function get_differences($array){
$new = array();
foreach($array as $k=>$a){
if(isset($array[$k-1])){
$new[$a['Date']] = $a['Value']-$array[$k-1]['Value'];
}
}
return $new;
}
To make sure your dates are in the same order as the keys, sort it by date first using usort function:
function sortdate($a, $b) {
return strtotime($a['Date']) - strtotime($b['Date']);
}
usort($array, 'sortdate');
$new = get_differences($array);
echo "<pre>";
print_r($new);
echo "</pre>";
Output:
Array
(
[2015-04-16 12:04:21] => 2
[2015-05-16 12:04:21] => 5
[2015-06-16 12:04:21] => 6
)
if your array = $temp
you may do this
$temp_2 = array();
$result = array();
foreach($temp as $k => $arr){
if(!count($temp_2)){
$temp_2 = $arr;
continue;
}
$t = array();
$t[Date] = $arr[Date];
$t[Value] = $arr[Value] - $temp_2[Value];
$result[] = $t;
}
Created new array and it will give you all intervals.
<?php
$arr = array(
array(
'Mark' => 0,
'ID' => 6236,
'Date' => '2015-03-16 12:04:21',
'Value' => '2'
),
array(
'Mark' => 0,
'ID' => 6236,
'Date' => '2015-04-16 12:04:21',
'Value' => '4'
),
array(
'Mark' => 0,
'ID' => 6236,
'Date' => '2015-05-16 12:04:21',
'Value' => '9'
),
array(
'Mark' => 0,
'ID' => 6236,
'Date' => '2015-06-16 12:04:21',
'Value' => '15'
),
);
echo '<pre>';
print_r($arr);
$newArr = array();
$i=0;
foreach($arr as $eachArr){
foreach($eachArr as $k => $v){
$newArr[$i][$k] = $v;
if($i==0){
$newArr[$i]['interval'] = 0;
}else{
$newArr[$i]['interval'] = $newArr[$i][$k] - $newArr[$i-1][$k];
}
}
$i++;
}
echo '<<br>';
print_r($newArr);

Sum of array for same values

I have this array:
Array (
[0] => Array (
[TaxeName] => TPS
[TaxeAmount] => 7
[Price] => 14
)
[1] => Array (
[TaxeName] => TVQ
[TaxeAmount] => 9.975
[Price] => 10
)
[2] => Array (
[TaxeName] => TVQ
[TaxeAmount] => 9.975
[Price] => 18
)
)
How I can get another array:
- Grouping the TaxeName and the TaxeAmount and
- Making the sum of the amount Price ?
Like this:
Array (
[0] => Array (
[TaxeName] => TPS
[TaxeAmount] => 7
[Price] => 14
)
[1] => Array (
[TaxeName] => TVQ
[TaxeAmount] => 9.975
[Price] => 28
)
)
It can be done with a nested foreach:
$original = array(.......); // your current array
$newArr = array();
foreach($original as $origVal){
$exists = false;
foreach($newArr as $key => $newVal){
if($newVal['TaxeName'] == $origVal['TaxeName']){
$newArr[$key]['Price'] += $origVal['Price'];
$exists = true;
break;
}
}
if(!$exists){
$newArr[] = $origVal;
}
}
Outputs
Array
(
[0] => Array
(
[TaxeName] => TPS
[TaxeAmount] => 7
[Price] => 14
)
[1] => Array
(
[TaxeName] => TVQ
[TaxeAmount] => 9.975
[Price] => 28
)
)
Something like this maybe:
$final = array();
foreach ($arr as $subarr)
{
if (!isset($final[$subarr['TaxeName']]))
{
$final[$subarr['TaxeName']] = array('Price' => 0);
}
$final[$subarr['TaxeName']]['TaxeName'] = $subarr['TaxeAmount'];
$final[$subarr['TaxeName']]['TaxeName'] = $subarr['TaxeName'];
$final[$subarr['TaxeName']]['Price'] = $final[$subarr['TaxeName']]['Price'] + $subarr['Price'];
}
The idea is to make a new array with the values that you need and initializing it with the price set to 0 so you can increment it in the foreach
You can do it like this also:
<?php
// Source
$myData = array(
array(
'TaxeName' => 'TPS',
'TaxeAmount' => 7,
'Price' => 14,
),
array(
'TaxeName' => 'TVQ',
'TaxeAmount' => 9.975,
'Price' => 10,
),
array(
'TaxeName' => 'TVQ',
'TaxeAmount' => 9.975,
'Price' => 18,
)
);
// Helper Function
function parseData($data)
{
$parsedData = array();
if (is_array($data)) {
foreach ($data as $dataItem) {
if (!array_key_exists($dataItem['TaxeName'], $parsedData)) {
$parsedData[$dataItem['TaxeName']] = array(
'TaxeName' => $dataItem['TaxeName'],
'TaxeAmount' => $dataItem['TaxeAmount'],
'Price' => floatval($dataItem['Price'])
);
} else {
$parsedData[$dataItem['TaxeName']]['Price'] += floatval($dataItem['Price']);
}
}
}
return array_values($parsedData);
}
// Test
$parsed_data = parseData($myData);
var_dump($parsed_data);
?>
Outputs:
Just use this function, input is your array, output is the array you required:
function calculate(array $input){
$output = [];
foreach ($input as $v){
if (array_key_exists($v['TaxeName'], $output)){
$output[$v['TaxeName']]['Price'] += $v['Price'];
} else {
$output[$v['TaxeName']] = $v;
}
}
return array_values($output);
}
I know that solutions with external classes are not welcome here on SO. I'm showing this just to illustrate the "Grouping the TaxeName and the TaxeAmount" task.
//use class tablearray from https://github.com/jspit-de/tableArray
$data = [
['TaxeName' => 'TPS', 'TaxeAmount' => 7, 'Price' => 14],
['TaxeName' => 'TVQ', 'TaxeAmount' => 9.975, 'Price' => 10],
['TaxeName' => 'TVQ', 'TaxeAmount' => 9.975, 'Price' => 18],
['TaxeName' => 'TVQ', 'TaxeAmount' => 19.975, 'Price' => 23]
];
$newData = tableArray::create($data)
->filterGroupSum('Price',['TaxeName','TaxeAmount'])
->fetchAll()
;
echo '<pre>';
var_export($newData);
Output:
array (
0 =>
array (
'TaxeName' => 'TPS',
'TaxeAmount' => 7,
'Price' => 14,
),
1 =>
array (
'TaxeName' => 'TVQ',
'TaxeAmount' => 9.975,
'Price' => 28,
),
2 =>
array (
'TaxeName' => 'TVQ',
'TaxeAmount' => 19.975,
'Price' => 23,
),
)

Categories