Combine two array and add value of array count - php

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

Related

PHP array sort by number of items

I need to sort my array-based on the number of elements in the inner array.
It's my current array.
Array
(
[0] => Array
(
)
[1] => Array
(
[0] => 2-3
)
[2] => Array
(
[0] => 5-9
)
[3] => Array
(
[0] => 5-9
[1] => 2-3
)
)
I need to sort the array by this format.
Array
(
[3] => Array
(
[0] => 5-9
[1] => 2-3
)
[2] => Array
(
[0] => 5-9
)
[1] => Array
(
[0] => 2-3
)
[0] => Array
(
)
)
This seems to do it:
<?php
$a = [
[], ['2-3'], ['5-9'], ['5-9', '2-3']
];
$f = fn ($b, $c) => count($c) <=> count($b);
usort($a, $f);
print_r($a);
https://php.net/function.usort
i hope this answer help you
$result = array();
$sorter = array();
foreach($values as $key => $vals){
$sorter[$key]= count($vals);
}
arsort($sorter);
foreach ($sorter as $ii => $va) {
$result[$ii]=$values[$ii];
}
output

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] =>
)
)
)

Assigning an array as a value of another array

I have three arrays i need to create an array which can be multidimensional.
1st array
Array
(
[0] => Test_One
[1] => Test_two
)
2nd array
Array
(
[0] => www.link.com
[1] => www.link2.com
)
3rd array
Array
(
[0] => Song1
[1] => song2
)
What i want
Array
(
[www.link.com] => Array
(
[0] => Test_one
[1] => Song1
)
[www.link2.com] => Array
(
[0] => Test_two
[1] => Song2
)
)
Assuming that you have same number of elements in all three arrays:
<?php
$arr1 = Array
(
0 => "Test_One",
1 => "Test_two"
);
$arr2 = Array
(
0 => "www.link.com",
1 => "www.link2.com"
);
$arr3 = Array
(
0 => "Song1",
1 => "Song2"
);
$final = []; //for versions below PHP 5.4 use $final = array();
foreach($arr2 as $key=>$value) {
$final[$value] = [$arr1[$key],$arr3[$key]];
}
print_r($final);
will output:
Array
(
[www.link.com] => Array
(
[0] => Test_One
[1] => Song1
)
[www.link2.com] => Array
(
[0] => Test_two
[1] => Song2
)
)
Update: Simplified foreach loop. From Comments #uchiha
Assuming that you havn't same number of elements in all three arrays:
<?php
$arr1 = Array
(
0 => "Test_One",
1 => "Test_two"
);
$arr2 = Array
(
0 => "www.link.com",
1 => "www.link2.com"
);
$arr3 = Array
(
0 => "Song1",
);
$final = []; //for versions below PHP 5.4 use $final = array();
foreach($arr2 as $key=>$value) {
if(array_key_exists($key,$arr1)) {
$final[$value][] = $arr1[$key];
}
if(array_key_exists($key,$arr3)) {
$final[$value][] = $arr3[$key];
}
}
print_r($final);
Output:
Array
(
[www.link.com] => Array
(
[0] => Test_One
[1] => Song1
)
[www.link2.com] => Array
(
[0] => Test_two
)
)
Hope this help :)
<?php
$array1 = Array
(
'Test_One',
'Test_two'
);
$array2 = Array
(
'www.link.com',
'www.link2.com'
);
$array3 = Array
(
'Song1',
'song2'
);
$array4 = array();
$i = 0;
foreach ($array2 as $a2){
$array4[$a2][] = $array1[$i];
$array4[$a2][] = $array3[$i];
$i++;
}
echo "<pre>";
print_r($array4);
?>

How to sum the numbers of two arrays

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

php unique multidimensional array by keeping entry with the highes value from one dimension?

I have another array unique question in the endless list of questions about them.
I can imagine this problem is quite simple to solve but I simply do not come on it.
Just because there are so many questions on this subject i wasn't able to find anything useful in this case.
the array:
Array
(
[0] => Array
(
[0] => blabla values
[1] => 91.181818181818
)
[1] => Array
(
[0] => blabla same values
[1] => 95.333333333333
)
[2] => Array
(
[0] => blabla other values
[1] => 86
)
[3] => Array
(
[0] => blabla other values
[1] => 92.5
)
[4] => Array
(
[0] => blabla same values
[1] => 88.5
)
)
I want to unique the array by the first array dimension and only keep the entry with the highest value from the second.
Maybe in MYSQL this would be no big deal but at the moment i am not able to implement something like that in php.
desired output array would be:
Array
(
[0] => Array
(
[0] => blabla values
[1] => 91.181818181818
)
[1] => Array
(
[0] => blabla same values
[1] => 95.333333333333
)
[2] => Array
(
[0] => blabla other values
[1] => 92.5
)
)
Has anyone a clever idea?
<?php
$list = array(
array('blabla values',91.181818181818),
array('blabla same values', 95.333333333333),
array('blabla other values', 86),
array('blabla other values', 92),
array('blabla same values', 88.5),
);
$result = array();
foreach ($list as $item)
{
$key = $item[0];
$value = $item[1];
if (!isset($result[$key]) || $result[$key][1] < $value)
{
$result[$key] = $item;
}
}
$result = array_values($result);
print_r($result);
the output:
Array
(
[0] => Array
(
[0] => blabla values
[1] => 91.1818181818
)
[1] => Array
(
[0] => blabla same values
[1] => 95.3333333333
)
[2] => Array
(
[0] => blabla other values
[1] => 92
)
)
usort($arr, function ($a, $b){
return $a[1] - $b[1];
});
$out = array();
foreach ($arr as $key => $value){
$out[$value[0]] = $value[1];
}
$arr = array_map(NULL, array_keys($out), $out);
Output:
Array
(
[0] => Array
(
[0] => blabla same values
[1] => 95.333333333333
)
[1] => Array
(
[0] => blabla other values
[1] => 86
)
[2] => Array
(
[0] => blabla values
[1] => 91.181818181818
)
)

Categories