How to sum the numbers of two arrays - php

This is probably not so hard, but I have searched for a while with no luck so any help will be appreciated.
I'm working in PHP with two arrays which contain a number of multidimensional arrays each with a set of two values. The 2 arrays looks something like this:
Array
(
[0] => Array
(
[0] => 3206.63
[1] => 815.47
)
[1] => Array
(
[0] => 5024.71
[1] => 803.73
)
[2] => Array
(
[0] => 3290.36
[1] => 625.02
)
//...
)
Array
(
[0] => Array
(
[0] => 3138.87
[1] => 819.8
)
[1] => Array
(
[0] => 5000.24
[1] => 810.87
)
[2] => Array
(
[0] => 3221.15
[1] => 668.58
)
//...
)
And I need to achieve this:
Array
(
[0] => Array
(
[0] => 6345.5
[1] => 1635.27
)
[1] => Array
(
[0] => 10024.95
[1] => 1614.6
)
[2] => Array
(
[0] => 6511.51
[1] => 1293.6
)
//...
)

This should work for you:
(Here i loop though each innerArray with foreach and use array_sum() to get the sum of all values of each innerArray)
<?php
$arr = array(
array(
3206.63,
815.47
),
array(
5024.71,
803.73
),
array(
3290.36,
625.02
)
);
$result = array();
foreach($arr as $v)
$result[] = array_sum($v);
print_r($result);
?>
Output:
Array ( [0] => 4022.1 [1] => 5828.44 [2] => 3915.38 )
If you have 2 Arrays you can use this:
$result = array();
foreach($arr1 as $k => $v)
$result[] = array_sum($arr1[$k]) + array_sum($arr2[$k]);
print_r($result);
EDIT:
From your updated question, this should work for you:
<?php
$arr1 = array(
array(
3206.63,
815.47
),
array(
5024.71,
803.73
),
array(
3290.36,
625.02
)
);
$arr2 = array(
array(
3138.87,
819.8
),
array(
5000.24,
810.87
),
array(
3221.15,
668.58
)
);
$sums = array();
foreach ($arr1 as $key => $value) {
$sums[$key][] = $arr1[$key][0] + $arr2[$key][0];
$sums[$key][] = $arr1[$key][1] + $arr2[$key][1];
}
print_r($sums);
?>
Output:
Array
(
[0] => Array
(
[0] => 6345.5
[1] => 1635.27
)
[1] => Array
(
[0] => 10024.95
[1] => 1614.6
)
[2] => Array
(
[0] => 6511.51
[1] => 1293.6
)
)

Related

Find Key By Value In Array Inside Array Using Php

Hey Dear I am Making Referal System With 11 Level Of Referal So Ho Can I Show All Referals And There Level I Have Tried
Array
(
[L1] => Array
(
[0] => TL422632
[1] => TL626461
)
[L2] => Array
(
[0] => TL4321
[1] => TL191123
)
[L3] => Array
(
[0] => TL555938
)
[L4] => Array
(
[0] => TL197752
)
[L5] => Array
(
[0] => TL835309
)
[L6] => Array
(
[0] => TL495903
)
[L7] => Array
(
[0] => TL207447
)
[L8] => Array
(
[0] => TL427427
)
[L9] => Array
(
[0] => TL288884
)
[L10] => Array
(
[0] => TL251399
)
[L11] => Array
(
[0] => TL284394
)
)
But I Don't Know Ho To Get L Number By Value For Example I Have TL284394 And I Want To Know Level So It Will Show L11 How Can I Do It
you can use two foreach loop inside each other and array_search() like this:
$a = array("L1" => [ 0 => "TL422632" ],"L2"=> [ 0 => "TL422635" ]);
$x = "TL422635";
foreach($a as $key=>$value){
if($x === $value){
return $key;
}else{
foreach($value as $k=>$v){
if($x === $v){
return array_search([$k=>$v],$a);
}
}
}
}

How can i rearrange a php array without using key names

My array is below i need to arrange like array2 (without use $aa['caption1'] like names directly)
arrray1 is
Array
(
[0] => Array
(
[caption1] => Array
(
[0] => gfdhgfjhg
[1] => dfhfgjghk
)
[caption2] => Array
(
[0] => shgjgh
[1] => dhfgkgjl
)
[banner_image] => Array
(
[0] => assets/images/page_content/img_namT7.jpg
[1] => assets/images/page_content/img_R8mzP.jpg
)
)
[1] => Array
(
[heading] => Array
(
[0] =>
)
[pragraph] => Array
(
[0] =>
)
)
)
arrray2 is(Required format )
Array
(
[0] => Array
(
array('caption1'=>'caption1','caption2'=>'shgjgh','banner_image'=>'assets/images/page_content/img_namT7.jpg'),
array('caption1'=>'dfhfgjghk','caption2'=>'dhfgkgjl','banner_image'=>'page_content/img_R8mzP.jpg')
)
[1] => Array
(
array('heading'=>'','pragraph'=>''),
array('heading'=>'fgh','pragraph'=>'ghgh'),
)
)
please any one help me.
The solution using array_keys, array_map and array_combine functions:
// $arr is your initial array
$result = [];
foreach($arr as $v){
$keys = array_keys($v);
$data = call_user_func_array('array_map', [null] + $v);
$result[] = array_map(function($item) use($keys){
return array_combine($keys, $item);
}, $data);
}
print_r($result);
The output:
Array
(
[0] => Array
(
[0] => Array
(
[caption1] => gfdhgfjhg
[caption2] => shgjgh
[banner_image] => assets/images/page_content/img_namT7.jpg
)
[1] => Array
(
[caption1] => dfhfgjghk
[caption2] => dhfgkgjl
[banner_image] => assets/images/page_content/img_R8mzP.jpg
)
)
[1] => Array
(
[0] => Array
(
[heading] =>
[pragraph] =>
)
)
)

Combine two array and add value of array count

I have two array like this
$arr1 = array('Prabhash', 'Nagda', 'Sayyed','Prabhash');
$arr2 = array('4', '1', '2','5');
echo "<pre>";
print_r($arr1);
print_r($arr2);
And I want output like this
Array
(
[0] => Array
(
[0] =>Prabhash
[1] =>9
)
[1] => => Array
(
[0] =>Nagda
[1] =>1
)
[2] => => Array
(
[0] =>Sayyed
[1] =>2
)
)
I have tried to combine and merge array but not success, Hope someone will help me for this better.
PHP code demo
<?php
$arr1 = array('Prabhash', 'Nagda', 'Sayyed','Prabhash');
$arr2 = array('4', '1', '2','5');
$result=array();
foreach($arr1 as $key => $value)
{
if(isset($result[$value]))
{
$result[$value][1]+=$arr2[$key];
}
else
{
$result[$value]=array($value,$arr2[$key]);
}
}
$result= array_values($result);
print_r($result);
Output:
Array
(
[0] => Array
(
[0] => Prabhash
[1] => 9
)
[1] => Array
(
[0] => Nagda
[1] => 1
)
[2] => Array
(
[0] => Sayyed
[1] => 2
)
)
Short solution using array_map, array_keys, array_flip, array_unique, array_intersect_key and array_sum functions:
$arr1 = array('Prabhash', 'Nagda', 'Sayyed','Prabhash');
$arr2 = array('4', '1', '2','5');
$result = array_map(function($n) use($arr1, $arr2){
$sum = array_sum(array_intersect_key($arr2, array_flip(array_keys($arr1, $n))));
return [$n, $sum];
}, array_unique($arr1));
print_r($result);
The output:
Array
(
[0] => Array
(
[0] => Prabhash
[1] => 9
)
[1] => Array
(
[0] => Nagda
[1] => 1
)
[2] => Array
(
[0] => Sayyed
[1] => 2
)
)
Try it.
<?php
$arr1 = array('Prabhash', 'Nagda', 'Sayyed','Prabhash');
$arr2 = array('4', '1', '2','5');
$newArray = array();
foreach($arr1 as $key => $value) {
$newArray[$value][0] =$value;
if(!isset($newArray[$value][1]) || $newArray[$value][1] == null)
$newArray[$value][1] = $arr2[$key];
else
$newArray[$value][1] = $newArray[$value][1]+$arr2[$key];
}
$newArray = array_values($newArray);
echo "<pre>";
print_r($newArray);
?>
OUTPUT :
Array
(
[0] => Array
(
[0] => Prabhash
[1] => 9
)
[1] => Array
(
[0] => Nagda
[1] => 1
)
[2] => Array
(
[0] => Sayyed
[1] => 2
)
)

Change key of multidimensional array

I have following array as response from db. I am trying to convert this database response into multidimensional array as per my requirement.
Array
(
[0] => Array
(
[0] => Array
(
[_id] => C10359
[AE] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
)
[1] => Array
(
[_id] => C10428
[AE] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
[1] => Array
(
[0] => Array
(
[_id] => C10350
[AE] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
)
[1] => Array
(
[_id] => C10430
[AE] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
)
Now I need to convert above array in following way.
Array
(
[0] => Array
(
[C10359] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
[C10428] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
[1] => Array
(
[C10350] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
[C10430] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
following is way i am trying
array_map(function($arr) {
return $arr[0] ;
},$panel_result);
But it is not working.
Kindly suggest how can I convert in required formate.
This should do the trick :
$arr = array(
array(
array(
'_id' => 'C10359',
'AE' => array
(
89785,
89786,
89857,
89859,
),
),
array(
'_id' => 'C10428',
'AE' => array
(
50191,
50203,
50230,
50244,
),
),
),
);
$output = array();
foreach ($arr as $levelK => $level) {
if(!isset($output[$levelK])){
$output[$levelK] = array();
}
foreach ($level as $subLevel) {
$id = $subLevel['_id'];
if (!isset($output[$levelK][$id])) {
$output[$levelK][$id] = array();
}
foreach ($subLevel['AE'] as $val) {
$output[$levelK][$id][] = $val;
}
}
}
Hope this helps.
Use array_column() and pass third param as the index key.
$reqArray = array();
foreach ($yourArray as $key => $innerArray) {
$reqArray[] = array_column($innerArray, 'AE', '_id');
}
OR
Use array map()
$reqArray = array_map(function($a){
return array_column($a, 'AE', '_id');
},$arr);

array manipulation and rearranging

Here's my deal.
I have this array:
Array // called $data in my code
(
[0] => Array
(
[name] => quantity
[value] => 0
)
[1] => Array
(
[name] => var_id
[value] => 4
)
[2] => Array
(
[name] => quantity
[value] => 0
)
[3] => Array
(
[name] => var_id
[value] => 5
)
)
which I need it to be like:
Array // called $temp in my code
(
[0] => Array
(
[0] => Array
(
[name] => quantity
[value] => 0
)
[1] => Array
(
[name] => var_id
[value] => 4
)
)
[2] => Array
(
[0] => Array
(
[name] => quantity
[value] => 0
)
[1] => Array
(
[name] => var_id
[value] => 5
)
)
)
and I did it using this code I made:
$data = $_POST['data'];
$temp = array();
foreach($data as $key => $datum)
{
if($key%2 == 0)
{
$temp[$key] = array();
array_push($temp[$key], $datum, $data[$key+1]);
}
}
But I think that my code is some kinda stupid, specially if I have a huge data.
eventually what I want to do is just have each two indexes combined in one array, and I know that there should be something better than my code to do it, any suggestions?
Discover array_chunk()
$temp = array_chunk($data, 2);
$cnt = count($data);
$temp = array();
for ($i = 0; $i < $cnt; $i = $i + 2)
{
$temp[] = array($data[$i], $data[$i+1]);
}
Take a look at array_chunk.
<?php
$array = array(
array(1),
array(2),
array(3),
array(4),
);
print_r(
array_chunk($array, 2, false)
);
/*
Array
(
[0] => Array
(
[0] => Array
(
[0] => 1
)
[1] => Array
(
[0] => 2
)
)
[1] => Array
(
[0] => Array
(
[0] => 3
)
[1] => Array
(
[0] => 4
)
)
)
*/

Categories