php:array sum without foreach - php

I have an array like this
Array
(
[1_DAY_2017] => Array
(
[SAMSUNG] => Array
(
[0] => 549
[1] => 199
[2] => 999
)
[XIAOMI] => Array
(
[0] => 199
[1] => 2999
[2] => 499
)
)
[2_DAY_2017] => Array
(
[SAMSUNG] => Array
(
[0] => 699
[1] => 999
)
[LENOVO] => Array
(
[0] => 280
[1] => 2550
[2] => 849
)
)
[3_DAY_2017] => Array
(
[OPPO] => Array
(
[0] => 500
[1] => 599
)
[SAMSUNG] => Array
(
[0] => 799
)
)
[4_DAY_2017] => Array
(
[SAMSUNG] => Array
(
[0] => 1299
[1] => 499
[2] => 799
[3] => 2500
)
[OPPO] => Array
(
[0] => 299
[1] => 349
[2] => 499
)
)
[5_DAY_2017] => Array
(
[XIAOMI] => Array
(
[0] => 500
[1] => 270
[2] => 340
)
[VIVO] => Array
(
[0] => 4599
[1] => 299
)
)
[6_DAY_2017] => Array
(
[VIVO] => Array
(
[0] => 240
[1] => 1899
[2] => 759
[3] => 530
)
[OPPO] => Array
(
[0] => 999
)
)
[7_DAY_2017] => Array
(
[OPPO] => Array
(
[0] => 300
[1] => 252
[2] => 1290
)
[LENOVO] => Array
(
[0] => 570
[1] => 1300
[2] => 666
)
)
)
From this i want to get an array
$output= [SAMSUNG => 9341, XIAOMI => 4807]
Here each item contains sum of the items in nested array.
Currently my solution conatins more than 2 for each loops but is there any way to optimize this??

You can use array_sum, array_map and array_column functions to get the result
array_sum(array_map('array_sum', array_column($a, 'SAMSUNG')))

With single array_reduce function:
// $arr is your initial array
$result = array_reduce($arr, function($r, $v){
if (isset($v['SAMSUNG'])) $r['SAMSUNG'] += array_sum($v['SAMSUNG']);
if (isset($v['XIAOMI'])) $r['XIAOMI'] += array_sum($v['XIAOMI']);
return $r;
}, ['SAMSUNG' => 0, 'XIAOMI' => 0]);

$sumArray = array();
foreach ($YourArray as $k=>$subArray) {
foreach ($subArray as $id=>$value) {
$sumArray[$id]+=$value;
}
}
print_r($sumArray);

Related

Adding elements from first array to second array

I have 2 arrays with me
First array
Array ( [2019-04-29] => Array ( [0] => 366 [1] => 82 [2] => 44 ) [2019-04-30] => Array ( [0] => 330 [1] => 115 [2] => 55 ) )
Second array
Array ( [0] => 492 [1] => 500 )
Need to merge these arrays with output like this.
Array ( [0] => Array ( [0] => 492 [1] => 366 [2] => 82 [3] => 44 ) [1] => Array ( [0] => 500 [1] => 330 [2] => 115 [3] => 55 ) )
Please help.
You can use array_walk and array_merge
$arr1 = Array (
'2019-04-29' => Array (
'0' => 366,
'1' => 82,
'2' => 44
) ,
'2019-04-30' => Array (
'0' => 330,
'1' => 115,
'2' => 55
)
);
$arr2 = Array ( '0' => 492 ,'1' => 500 ) ;
$index = 0;
$res=[];
array_walk($arr1, function($v,$k) use (&$res,$arr2,&$index){
$res[] = array_merge(array($arr2[$index]),$v);
$index++;
});
echo '<pre>';
print_r($res);
Result
Array
(
[0] => Array
(
[0] => 492
[1] => 366
[2] => 82
[3] => 44
)
[1] => Array
(
[0] => 500
[1] => 330
[2] => 115
[3] => 55
)
)
You can do this with a foreach loop and array_merge, after using array_values to reindex $array1 to numeric indexes starting with 0.
$array1 = array_values($array1);
foreach ($array2 as $k => &$v) {
$v = array_merge(array($v), $array1[$k]);
}
print_r($array2);
Output:
Array
(
[0] => Array
(
[0] => 492
[1] => 366
[2] => 82
[3] => 44
)
[1] => Array
(
[0] => 500
[1] => 330
[2] => 115
[3] => 55
)
)
Demo on 3v4l.org
You can use array_walk and array_merge combination with traditional incrementer
array_walk($arr1, function (&$item, $key) use ($arr2,&$i) { // $i should change at memory address
$item = array_merge($item, [$arr2[$i]]);
$i++;
});
Output
Array
(
[2019-04-29] => Array
(
[0] => 366
[1] => 82
[2] => 44
[3] => 492
)
[2019-04-30] => Array
(
[0] => 330
[1] => 115
[2] => 55
[3] => 500
)
)
Demo.

Parse a multi dimensional array and pull values

How can i parse the below multi dimensional array ($array) and pull values of [productType] , [totalPrice]and [productCategory] if [packageCode] is matching with the value of $pkgcodes[1]...[z]
$pkgcodes is an array of codes
print_r of $pkgcodes
Array ( [0] => TRA1I2 [1] => TREZEC [n] ...)
The array $array is a response from SOAP client
print_r of $array
Array (
[0] => Array (
[packageCode] => TRA1I2
[totalPrice] => 17
[productType] => product Only
[products] => Array (
[0] => Array (
[productCategory] => Simple
[paxes] => Array (
[0] => Array (
[paxType] => Adult
[age] => 30 )
[1] => Array (
[paxType] => Adult
[age] => 30 ) )
[totalproductRate] => 17
[ratesPerNight] => Array (
[0] => Array (
[date] => 2015-01-28
[amount] => 17 ) ) ) ) )
[1] => Array (
[packageCode] => TREZEC
[totalPrice] => 17
[productType] => product Only
[products] => Array (
[0] => Array (
[productCategory] => Complicated
[paxes] => Array (
[0] => Array (
[paxType] => Adult
[age] => 30 )
[1] => Array (
[paxType] => Adult
[age] => 30 ) )
[totalproductRate] => 17
[ratesPerNight] => Array (
[0] => Array (
[date] => 2015-01-28
[amount] => 17 ) ) ) ) ) ).
You help is more appreciated
Try with -
$newArr = array();
foreach($array as $value) {
if (in_array($value['packageCode'], $pkgcodes)) {
$temp['productType'] = $value['productType'];
$temp['totalPrice'] = $value['totalPrice'];
$temp['packageCode'] = $value['packageCode'];
$temp['productCategory'] = $value['products']['productCategory'];
$newArr[] = $temp;
}
}
var_dump($newArr);

determine average value from an array column

consider below array:-
Array
(
[0] => Array
(
[0] => 99895
[1] => 35378
[2] => 0.01
)
[1] => Array
(
[0] => 99895
[1] => 813
[2] => -0.97
)
[2] => Array
(
[0] => 99895
[1] => 771
[2] => 0.29
)
[3] => Array
(
[0] => 442
[1] => 833
[2] => -1.06
)
[4] => Array
(
[0] => 442
[1] => 485
[2] => -0.61
)
[5] => Array
(
[0] => 442
[1] => 367
[2] => -0.14
)
[6] => Array
(
[0] => 442
[1] => 478
[2] => 0.77
)
[7] => Array
(
[0] => 442
[1] => 947
[2] => -0.07
)
[8] => Array
(
[0] => 7977
[1] => 987
[2] => 0.76
)
[9] => Array
(
[0] => 7977
[1] => 819
[2] => 0.37
)
[10] => Array
(
[0] => 7977
[1] => 819
[2] => 0.36
)
[11] => Array
(
[0] => 7977
[1] => 653
[2] => 1.16
)
[12] => Array
(
[0] => 7977
[1] => 1653
[2] => 1.15
)
)
from the above array how will I determine the below array?
array
(
99895 => -0.223
442 => -0.22
7977 => 0.76
)
Actually I need the average value of column 3 in respect of column 1.
First collect all the column 3 elements into an array keyed off column 1:
$arrays = array();
foreach ($input as $vals) {
$key = $vals[0];
$val = $vals[2];
if (isset($arrays[$key])) {
$arrays[$key][] = $val;
} else {
$arrays[$key] = array($val);
}
}
Now go through all of them, calculating the averages:
foreach ($arrays as &$array) {
$array = array_sum($array)/count($array);
}

how to loop through an array within an array in php

Here's the array structure (only the first element in the array):
Array
(
[1] => Array
(
[pageid] => 1
[step_order] => 1
[pageurl] => http://www.domain.com/
[in_links] => Array
(
[domains] => Array
(
[Direct Entry] => 1520
[www.google.com] => 387
[www.google.co.in] => 14
[search.yahoo.com] => 10
[All other] => 27
)
[impressions] => Array
(
[Direct Entry] => Array
(
[0] => 10654
[1] => 10728
[2] => 10772
)
[www.google.com] => Array
(
[0] => 10991
[1] => 12455
[2] => 12466
[3] => 10757
)
[www.google.co.in] => Array
(
[0] => 9839
[1] => 9837
[2] => 9845
)
[search.yahoo.com] => Array
(
[0] => 12087
[1] => 10864
)
)
)
[out_links] => Array
(
[domain] => Array
(
[Left site] => 1752
[http://www.domain.com/#] => 102
[http://www.domain.com/contact] => 102
[http://www.domain.com/#basic_inline_div] => 2
)
[impressions] => Array
(
[Left site] => Array
(
[0] => 7680
[1] => 9728
[2] => 10496
)
[http://www.domain.com/#] => Array
(
[0] => 259
[1] => 11013
)
[http://www.domain.com/contact] => Array
(
[0] => 12802
[1] => 10757
)
[http://www.domain.com/#basic_inline_div] => Array
(
[0] => 11
[1] => 51
)
)
)
[visitors] => 1958
)
)
I'm trying to loop to get the elements from domains, impressions (and sub elements). I managed to get the first parts: pageid, step_order, page_url. I'm having trouble with in_links and out_links and their child arrays. Anyone have ideas on how to pull that data?
Here's how you get the domains from in_links. The others are similar (but I'm not sure what the indexes in the impressions sub-array represent).
foreach ($array as $element) {
foreach ($element['in_links']['domains'] as $domain => $count) {
echo "Domain: $domain, Count: $count\n";
}
}

array search for a key=>value

I have an array,
$arr=(
[0] => Array
(
[groupid] => 1
[groupname] => Red
[members] => Array
(
[0] => Array
(
[mid] => 9
[name] => Anith
)
[1] => Array
(
[mid] => 11
[name] => Aravind
)
[2] => Array
(
[mid] => 10
[name] => Lekshmi
)
)
)
[1] => Array
(
[groupid] => 2
[groupname] => Blue
[members] => Array
(
[0] => Array
(
[mid] => 6
[name] => Yamuna
)
[1] => Array
(
[mid] => 2
[name] => Kamala K
)
[2] => Array
(
[mid] => 13
[name] => Sooraj K
)
)
)
I want to check [mid] => 2 is in the array..If it exists
I want to delete it(ie. unset the array )-----
[1] => Array
(
[mid] => 2
[name] => Kamala K
)
;;;
eg:--unset($arr[1]['members'][2];
This should do the trick
foreach ($arr as $group => $subarray) {
foreach ($subarray['members'] as $k => $v) {
if ($v['mid'] == 2) {
unset($arr[$group]['members'][$k]);
break;
}
}
}
var_dump($arr);
If you feel like getting crafty, you could do something like this:
// note: requires PHP >= 5.3
foreach ($arr as $key => &$value) {
$value['members'] = array_filter(
$value['members'],
function($member) {
return $member['mid'] != 2;
}
);
}
var_dump($arr);

Categories