Display a sum of two array values with same keys - php

I have two array with same keys this two array contain month wise data of my table. i want sum of this values and return same keys sum of values in one other array
Here is my two array
array1
Array ( [0] => Array ( [0] => Jan [1] => 0 )
[1] => Array ( [0] => Feb [1] => 22 )
[2] => Array ( [0] => Mar [1] => 0 )
[3] => Array ( [0] => Apr [1] => 9 )
[4] => Array ( [0] => May [1] => 1 )
[5] => Array ( [0] => Jun [1] => 0 )
[6] => Array ( [0] => Jul [1] => 0 )
[7] => Array ( [0] => Aug [1] => 0 )
[8] => Array ( [0] => Sep [1] => 0 )
[9] => Array ( [0] => Oct [1] => 0 )
[10] => Array ( [0] => Nov [1] => 0 )
[11] => Array ( [0] => Dec [1] => 0 )
)
array 2:
Array ( [0] => Array ( [0] => Jan [1] => 0 )
[1] => Array ( [0] => Feb [1] => 0 )
[2] => Array ( [0] => Mar [1] => 18 )
[3] => Array ( [0] => Apr [1] => 1 )
[4] => Array ( [0] => May [1] => 1 )
[5] => Array ( [0] => Jun [1] => 0 )
[6] => Array ( [0] => Jul [1] => 0 )
[7] => Array ( [0] => Aug [1] => 0 )
[8] => Array ( [0] => Sep [1] => 0 )
[9] => Array ( [0] => Oct [1] => 0 )
[10] => Array ( [0] => Nov [1] => 0 )
[11] => Array ( [0] => Dec [1] => 0 )
)
i also tried using this code
function sum_arrays($array1, $array2) {
$array = array();
foreach($array1 as $index => $value) {
$array[$index] = isset($array2[$index]) ? $array2[$index] + $value : $value;
}
return $array;
}
i want result like below
Array ( [0] => Array ( [0] => Jan [1] => 0 )
[1] => Array ( [0] => Feb [1] => 22 )
[2] => Array ( [0] => Mar [1] => 18 )
[3] => Array ( [0] => Apr [1] => 10 )
[4] => Array ( [0] => May [1] => 2 )
[5] => Array ( [0] => Jun [1] => 0 )
[6] => Array ( [0] => Jul [1] => 0 )
[7] => Array ( [0] => Aug [1] => 0 )
[8] => Array ( [0] => Sep [1] => 0 )
[9] => Array ( [0] => Oct [1] => 0 )
[10] => Array ( [0] => Nov [1] => 0 )
[11] => Array ( [0] => Dec [1] => 0 )
)

array_map approach (if 2 arrays have month items in same order):
// shortened array samples
$arr1 = [
["Jan", 0],
["Feb", 22],
["Mar", 0]
];
$arr2 = [
["Jan", 0],
["Feb", 0],
["Mar", 18]
];
$result = array_map(function($a, $b){
return [$a[0], $a[1] + $b[1]];
}, $arr1, $arr2);
print_r($result);
The output:
Array
(
[0] => Array
(
[0] => Jan
[1] => 0
)
[1] => Array
(
[0] => Feb
[1] => 22
)
[2] => Array
(
[0] => Mar
[1] => 18
)
)

Using The foreach Loop:
$sum = 0;
foreach($items as $item) {
$sum += $item['qty'];
}
echo $sum;
Using array_map():
echo array_sum(array_map(
function($item) {
return $item['qty'];
}, $items)
);
Using array_reduce():
echo array_reduce($items, function($carry, $item) {
$carry += $item['qty'];
return $carry;
});

Related

Convert Single Array to multidimensional

I new to PHP, I need to convert single dimension array into multi dimension array in php
I have data like this, need to minimize as below.
Array
(
[0] => Array
(
[0] => David
[1] => School
[2] => 19
[3] => 29
)
[1] => Array
(
[0] => Paul
[1] => Home
[2] => 19
[3] => 29
)
[2] => Array
(
[0] => Paul
[1] => Cinema
[2] => 19
[3] => 29
)
[3] => Array
(
[0] => Paul
[1] => Park
[2] => 19
[3] => 29
)
[4] => Array
(
[0] => Rossie
[1] => Playground
[2] => 19
[3] => 29
)
[5] => Array
(
[0] => Rossie
[1] => Hotel
[2] => 19
[3] => 29
)
[6] => Array
(
[0] => Rossie
[1] => Hospital
[2] => 19
[3] => 29
)
)
And I want convert it to multidimensional
Array
(
[0] => Array
(
[0] => Array
(
[0] => David
(
[0] => School
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
)
)
)
[1] => Array
(
[0] => Array
(
[0] => Paul
(
[0] => Home
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
[1] => Cinema
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
[1] => Park
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
)
)
)
[2] => Array
(
[0] => Array
(
[0] => Rossie
(
[0] => Playground
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
[1] => Hotel
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
[1] => Hospital
(
[0] => 19
[1] => 29
[2] => 39
[3] => 49
)
)
)
)
)
I hope you get an idea. But my function doesn't do this correctly or maybe there are other ways to do this easier ?
I would be grateful for any help.
Thanks
Below is one way to do it:
<?php
$arr = [array('David','School',19,29),
array('Paul','Home',19,29),
array('Paul','Cinema',19,29),
array('Paul','Park',19,29),
array('Rossie','Playground',19,29),
array('Rossie','Hotel',19,29),
array('Rossie','Hospital',19,29)];
// Get all names.
$names = array_unique(array_map(function($value){return $value[0];}, $arr));
$places = [];
// Create the multidimensional array, grouping by name.
foreach($names as $key => $name){
$tempArr = [];
foreach($arr as $record){
if($record[0] === $name){
$tempArr[][$record[1]] = array_slice($record,2);
}
}
$places[][][$name] = $tempArr;
}
print("<pre>".print_r($places,true)."</pre>");
This will return the following result:
Array
(
[0] => Array
(
[0] => Array
(
[David] => Array
(
[0] => Array
(
[School] => Array
(
[0] => 19
[1] => 29
)
)
)
)
)
[1] => Array
(
[0] => Array
(
[Paul] => Array
(
[0] => Array
(
[Home] => Array
(
[0] => 19
[1] => 29
)
)
[1] => Array
(
[Cinema] => Array
(
[0] => 19
[1] => 29
)
)
[2] => Array
(
[Park] => Array
(
[0] => 19
[1] => 29
)
)
)
)
)
[2] => Array
(
[0] => Array
(
[Rossie] => Array
(
[0] => Array
(
[Playground] => Array
(
[0] => 19
[1] => 29
)
)
[1] => Array
(
[Hotel] => Array
(
[0] => 19
[1] => 29
)
)
[2] => Array
(
[Hospital] => Array
(
[0] => 19
[1] => 29
)
)
)
)
)
)

Filling an array with missing values afterwards

I'm getting values for my flot chart via ajax.
At the backend script my test array looks like this:
Array
(
[0] => Array
(
[0] => 6
[1] => 1
)
[1] => Array
(
[0] => 7
[1] => 7
)
[2] => Array
(
[0] => 8
[1] => 37
)
[3] => Array
(
[0] => 9
[1] => 44
)
)
The value with the offset [0] represents the hour.
Now I need 24 array objects for every hour. How to maintain this without touching the given elements?
e.g.
Array
(
[0] => Array
(
[0] => 0
[1] => 0
)
[6] => Array
(
[0] => 6
[1] => 1
)
[...]
[7] => Array
(
[0] => 7
[1] => 7
)
[8] => Array
(
[0] => 8
[1] => 37
)
[9] => Array
(
[0] => 9
[1] => 44
)
Thanks in advance.
You want to "re-key" this result set according to the first subarray value, and then merge it into an array of 24 hour elements.
Try this:
$data = [
[6, 1],
[7, 7],
[8, 37],
[9, 44]
];
$hours = array_fill(0, 25, [0, 0]);
$data = array_combine(array_column($data, 0), $data);
$hours = array_replace($hours, $data);
print_r($hours);
This:
Creates a 24 element array $hours containing sub-arrays containing default values [0, 0]
Re-keys your $data, pulling out and using the first value of each sub-array to do so. This assumes you are using PHP 5.5.
Finally, replace the modified $data into $hours
This yields:
Array
(
[0] => Array
(
[0] => 0
[1] => 0
)
[1] => Array
(
[0] => 0
[1] => 0
)
[2] => Array
(
[0] => 0
[1] => 0
)
[3] => Array
(
[0] => 0
[1] => 0
)
[4] => Array
(
[0] => 0
[1] => 0
)
[5] => Array
(
[0] => 0
[1] => 0
)
[6] => Array
(
[0] => 6
[1] => 1
)
[7] => Array
(
[0] => 7
[1] => 7
)
[8] => Array
(
[0] => 8
[1] => 37
)
[9] => Array
(
[0] => 9
[1] => 44
)
[10] => Array
(
[0] => 0
[1] => 0
)
[11] => Array
(
[0] => 0
[1] => 0
)
[12] => Array
(
[0] => 0
[1] => 0
)
[13] => Array
(
[0] => 0
[1] => 0
)
[14] => Array
(
[0] => 0
[1] => 0
)
[15] => Array
(
[0] => 0
[1] => 0
)
[16] => Array
(
[0] => 0
[1] => 0
)
[17] => Array
(
[0] => 0
[1] => 0
)
[18] => Array
(
[0] => 0
[1] => 0
)
[19] => Array
(
[0] => 0
[1] => 0
)
[20] => Array
(
[0] => 0
[1] => 0
)
[21] => Array
(
[0] => 0
[1] => 0
)
[22] => Array
(
[0] => 0
[1] => 0
)
[23] => Array
(
[0] => 0
[1] => 0
)
)
Hope this helps :)
You can try also this:
// yours test array
$times = array(
array(6,1),
array(7,7),
array(8,37),
array(9,44)
);
// array with all hours
$fullTimes = array_fill(0, 24, array(0,0));
foreach ($times as $time) {
$fullTimes[$time[0]] = $time;
}
Solution for me:
while ($stmt->fetch()) {
$x = $hour;
$y = $zugriffe;
$data1[$x] = array ($x, $y);
}
for ($i=0;$i<=24;$i++){
if (!isset($data1[$i])){
$data1[$i] = array ($i,0);
}
}
sort($data1);
Assume the array's name is $a, you can do like this:
for($i = 0; $i < 24; $i++) {
if(isset($a[$i])) continue;
else {
$a[$i] = array($i,"something you like here");
}
}

How to change this kind of array arrangement into like this

I am getting data from Google Analytics API. The GA API returns these data:
Array
(
[0] => Array
(
[0] => 00
[1] => bing
[2] => 1
)
[1] => Array
(
[0] => 00
[1] => google
[2] => 12
)
[2] => Array
(
[0] => 00
[1] => yahoo
[2] => 1
)
[3] => Array
(
[0] => 01
[1] => google
[2] => 7
)
[4] => Array
(
[0] => 02
[1] => google
[2] => 5
)
[5] => Array
(
[0] => 03
[1] => bing
[2] => 1
)
[6] => Array
(
[0] => 03
[1] => google
[2] => 4
)
[7] => Array
(
[0] => 04
[1] => google
[2] => 7
)
[8] => Array
(
[0] => 05
[1] => google
[2] => 5
)
[9] => Array
(
[0] => 05
[1] => yahoo
[2] => 1
)
[10] => Array
(
[0] => 06
[1] => bing
[2] => 1
)
[11] => Array
(
[0] => 06
[1] => google
[2] => 2
)
[12] => Array
(
[0] => 07
[1] => google
[2] => 4
)
[13] => Array
(
[0] => 08
[1] => bing
[2] => 1
)
[14] => Array
(
[0] => 08
[1] => google
[2] => 8
)
[15] => Array
(
[0] => 09
[1] => bing
[2] => 4
)
[16] => Array
(
[0] => 09
[1] => google
[2] => 13
)
[17] => Array
(
[0] => 10
[1] => bing
[2] => 1
)
[18] => Array
(
[0] => 10
[1] => google
[2] => 19
)
[19] => Array
(
[0] => 10
[1] => yahoo
[2] => 1
)
[20] => Array
(
[0] => 11
[1] => bing
[2] => 1
)
[21] => Array
(
[0] => 11
[1] => google
[2] => 23
)
[22] => Array
(
[0] => 11
[1] => yahoo
[2] => 1
)
[23] => Array
(
[0] => 12
[1] => bing
[2] => 1
)
[24] => Array
(
[0] => 12
[1] => google
[2] => 18
)
[25] => Array
(
[0] => 13
[1] => bing
[2] => 1
)
[26] => Array
(
[0] => 13
[1] => google
[2] => 17
)
[27] => Array
(
[0] => 13
[1] => yahoo
[2] => 1
)
[28] => Array
(
[0] => 14
[1] => bing
[2] => 3
)
[29] => Array
(
[0] => 14
[1] => google
[2] => 30
)
[30] => Array
(
[0] => 14
[1] => yahoo
[2] => 2
)
[31] => Array
(
[0] => 15
[1] => google
[2] => 15
)
[32] => Array
(
[0] => 15
[1] => yahoo
[2] => 2
)
[33] => Array
(
[0] => 16
[1] => bing
[2] => 1
)
[34] => Array
(
[0] => 16
[1] => google
[2] => 22
)
[35] => Array
(
[0] => 16
[1] => yahoo
[2] => 1
)
[36] => Array
(
[0] => 17
[1] => google
[2] => 18
)
[37] => Array
(
[0] => 17
[1] => yahoo
[2] => 2
)
[38] => Array
(
[0] => 18
[1] => google
[2] => 15
)
[39] => Array
(
[0] => 19
[1] => bing
[2] => 1
)
[40] => Array
(
[0] => 19
[1] => google
[2] => 18
)
[41] => Array
(
[0] => 19
[1] => yahoo
[2] => 3
)
[42] => Array
(
[0] => 20
[1] => google
[2] => 15
)
[43] => Array
(
[0] => 21
[1] => google
[2] => 18
)
[44] => Array
(
[0] => 21
[1] => yahoo
[2] => 1
)
[45] => Array
(
[0] => 22
[1] => bing
[2] => 1
)
[46] => Array
(
[0] => 22
[1] => google
[2] => 21
)
[47] => Array
(
[0] => 23
[1] => google
[2] => 8
)
[48] => Array
(
[0] => 23
[1] => yahoo
[2] => 1
)
)
I want that array to form like this:
$example_data = array(
array('00',1,12,1),//bing,google,yahoo
array('01',0,7,0),
array('02',0,5,0),
array('03',1,4,0),
array('04',0,7,0),
array('05',0,5,1),
array('06',1,2,0),
array('07',0,7,0),
array('08',1,8,0),
array('09',0,13,0),
array('10',1,19,1),
//should have more arrays in here, but I hope you got my point...
);
So basically, all the 00 of bing,google, and yahoo is being grouped by on a single array, then the other 01,02,03,etc.. are to be grouped by on a single array again.
So if you noticed that we have a zero on this array array('01',0,7,0), it because bing doesn't have a 01 value in the array, and yahoo also doesn't have a 01 value in the array, except for google which has a 01 that has a value of 7.
Any help how to transform this kind of array to the one I posted.
Your help will be greatly appreciated! Thanks!
<?php
$array=json_decode('[["00","bing","1"],["00","google","12"],["00","yahoo","1"],["01","google","7"],["02","google","5"],["03","bing","1"],["03","google","4"],["04","google","7"],["05","google","5"],["05","yahoo","1"],["06","bing","1"],["06","google","2"],["07","google","4"],["08","bing","1"],["08","google","8"],["09","bing","4"],["09","google","13"],["10","bing","1"],["10","google","19"],["10","yahoo","1"],["11","bing","1"],["11","google","23"],["11","yahoo","1"],["12","bing","1"],["12","google","18"],["13","bing","1"],["13","google","17"],["13","yahoo","1"],["14","bing","3"],["14","google","30"],["14","yahoo","2"],["15","google","15"],["15","yahoo","2"],["16","bing","1"],["16","google","22"],["16","yahoo","1"],["17","google","18"],["17","yahoo","2"],["18","google","15"],["19","bing","1"],["19","google","18"],["19","yahoo","3"],["20","google","15"],["21","google","18"],["21","yahoo","1"],["22","bing","1"],["22","google","21"],["23","google","8"],["23","yahoo","1"]]');
$temp=array();
$temp1=array();
foreach($array as $arr){
if(array_search($arr[0], $temp)===FALSE)
$temp[]=$arr[0];
}
foreach($temp as $t){
$bing=0;
$google=0;
$yahoo=0;
foreach($array as $arr){
if($t==$arr[0]){
if($arr[1]=='bing'){
$bing=$arr[2];
}else if($arr[1]=='google'){
$google=$arr[2];
}else if($arr[1]=='yahoo'){
$yahoo=$arr[2];
}
}
}
$temp1[]=array($t,$bing,$google,$yahoo);
}
var_dump($temp1);
?>
I managed to answer it on my own, not sure if this is efficient but it works! :)
//$new variable is the array that I the google analytics api returned
foreach($new as $key => $value){
if($value[1] == 'bing')
$combine['bing'][$value[0]] = $value[2];
if($value[1] == 'google')
$combine['google'][$value[0]] = $value[2];
if($value[1] == 'yahoo')
$combine['yahoo'][$value[0]] = $value[2];
}
$example_data = array();
for($i=0;$i<=23;$i++){
$tempI = $i;
if($i < 10)
$tempI = "0".$i;
$bing = 0;
$google = 0;
$yahoo = 0;
if(isset($combine['bing'][$tempI])){
$bing = $combine['bing'][$tempI];
}
if(isset($combine['google'][$tempI])){
$google = $combine['google'][$tempI];
}
if(isset($combine['yahoo'][$tempI])){
$yahoo = $combine['yahoo'][$tempI];
}
//time,bing,google,yahoo
$example_data[$i][0] = $tempI;
$example_data[$i][1] = $bing;
$example_data[$i][2] = $google;
$example_data[$i][3] = $yahoo;
}
echo "<pre>";
print_r($example_data);
echo "</pre>";
Thanks again StackOverflow! :)
How about this:
<?php
$initial = [
[ '00', 'bing', 1 ],
[ '00', 'google', 12 ],
[ '00', 'yahoo', 1 ],
[ '01', 'google', 7 ],
[ '02', 'google', 5 ],
[ '03', 'bing', 1 ],
[ '03', 'google', 4 ],
];
$searchEngines = ['bing', 'google', 'yahoo' ];
$temp = [];
foreach ( $initial as $data )
{
// Create array of the type $data['00']['google'] = 12
$temp[ $data[0] ][ $data[1] ] = $data[2];
}
$final = [];
// Now go over the newly created array
foreach( $temp as $code => $entry )
{
// Initialize a new array holding initially that $code ( I don't know if $code is correct term though )
$temp2 = [ $code ];
// And now for each of the defined search engines
foreach( $searchEngines as $se )
{
// Check if we have a value set
if (isset( $entry[$se] ) )
{
// If we do - use it
$temp2[] = $entry[$se];
}
else
{
// Otherwise use 0
$temp2[] = 0;
}
}
// Set it to the final resulting array
$final[] = $temp2;
}
echo '<pre>';print_r( $final );echo '</pre>';
And if someday you decide to add another search engine to the list all you'll have to do will be to add it to the $searchEngines array.

building a multidimention array php

I generate this array $months from an awstats file that looks like this :
Array
(
[0] => Array
(
[0] => 10
[1] => 37
[2] => 11
)
[1] => Array
(
[0] => 11
[1] => 34
[2] => 19
)
[2] => Array
(
[0] => 12
[1] => 20
[2] => 11
)
)
Which $months[0][0] represent the number of a month and $months[0][1] it's data. What I'd like to do is to build an array that has all the number of months of the year with it's data and if it doesn't have one make it to 0.
Like this :
Array
(
[0] => Array
(
[0] => 01
[1] => 41
[2] => 52
)
[1] => Array
(
[0] => 02
[1] => 74
[2] => 66
)
[2] => Array
(
[0] => 03
[1] => 40
[2] => 83
)
[3] => Array
(
[0] => 04
[1] => 0
[2] => 0
)
[4] => Array
(
[0] => 05
[1] => 0
[2] => 0
)
[5] => Array
(
[0] => 06
[1] => 0
[2] => 0
)
and so on... Is there any way I can achieve this?Much appreciated!
Here's my idea (if I understood your question right):
First, create another array ($sortedMonths) with 12 elements and fill them with 'empty data':
$sortedMonths = array();
for ($i = 1; $i <= 12; $i++) {
$sortedMonths[] = array($i, 0, 0);
}
/*
Array
(
[0] => Array
(
[0] => 1,
[1] => 0,
[2] => 0
)
...
[11] => Array
(
[0] => 12,
[1] => 0,
[2] => 0
)
)
*/
Then loop through $months and assign data to the appropriate element of $sortedMonths:
foreach ($months as $month) {
$sortedMonths[$month[0]-1] = $month;
}

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);
}

Categories