php - how to sum multi-dimensional array - php

I have data below :
Array(
[A] => Array
(
[AA] => 10
)
[B] => Array
(
[BA] => 5
[BB] => 1
[BC] => -2
)
[C] => Array
(
[CA] => 3
[CB] => 0
)
)
I want to sum the value of second element my array (BA,BB,BC, etc) like this :
Array(
[A] => 10
[B] => 4
[C] => 3
)
I've tried to do with foreach (I'm using php as my platform) but the result is wrong, can someone give me explanation and the logic to solve this? thanks

You can loop thru your array and use array_sum
$arr = array(
"A" => array
(
"AA" => 10,
),
"B" => array
(
"BA" => 5,
"BB" => 1,
"BC" => -2
),
"C" => array
(
"CA" => 3,
"CB" => 0
)
);
$result = array();
foreach( $arr as $key => $val ){
$result[$key] = array_sum ( $val );
}
echo "<pre>";
print_r( $result );
echo "</pre>";
This will result to:
Array
(
[A] => 10
[B] => 4
[C] => 3
)
Doc: http://php.net/manual/en/function.array-sum.php

This should work for arrays like the one of your example:
$arr = array(
"A" => array
(
"AA" => 10,
),
"B" => array
(
"BA" => 5,
"BB" => 1,
"BC" => -2
),
"C" => array
(
"CA" => 3,
"CB" => 0
)
);
$res = array();
foreach($arr as $key => $value) {
foreach($value as $number) {
(!isset($res[$key])) ?
$res[$key] = $number :
$res[$key] += $number;
}
}
echo "<pre>";
print_r( $res );
echo "</pre>";
This is working without using an inbuilt function.

if you want sum column
<?php
$array = array
(
"A"=>array
(
"AA" => 10,
),
"B"=>array
(
"BA" => 5,
"BB" => 1,
"BC" => -2
),
"C"=>array
(
"CA" => 3,
"CB" => 0
)
);
foreach ($array as $key=>$value)
{
$mehrdad[]=$key;
}
foreach ($mehrdad as $key1=>$value1)
{
$arrays=$array[$value1];
foreach ($arrays as $key2=>$value2)
{
$mehrdadi[]=$key2;
}
}
$mehrdadend=array_unique($mehrdadi);
$mehrdadis = array();
foreach ($mehrdadend as $key3=>$value3)
{
$sum=array_sum(array_column($array, $value3));
$mehrdadis[$value3] = $sum;
}
print_r($mehrdadis);
?>
Result
Array
(
[AA] => 10
[BA] => 5
[BB] => 1
[BC] => -2
[CA] => 3
[CB] => 0
)

Related

return array containing duplicates php

i have a multidimensional array and i only want to keep the entries with the most duplicates. the closest i got was:
$wd = array_unique($arr);
$d = array_diff($arr, $wd);
print_r($d);
but that only works for single dimensional arrays and outputs all duplicates. how would i go about doing this?
examples of desired output:
if the array is:
array(
[1] => (
[u] => test1u
[d] => test1d
)
[2] => (
[u] => test2u
[d] => test2d
)
[3] => (
[3] => test3u
[3] => test3d
)
[1] => (
[u] => test1u
[d] => test1d
)
)
it should return array([1] => ( [u] => test1u [d] => test1d))
and if the array is:
array(
[1] => (
[u] => test1u
[d] => test1d
)
[2] => (
[u] => test2u
[d] => test2d
)
[3] => (
[3] => test3u
[3] => test3d
)
[1] => (
[u] => test1u
[d] => test1d
)
[2] => (
[u] => test2u
[d] => test2d
)
)
it should return array([1] => ( [u] => test1u [d] => test1d)[2] => ( [u] => test2u [d] => test2d))
but if the array is:
array(
[1] => (
[u] => test1u
[d] => test1d
)
[2] => (
[u] => test2u
[d] => test2d
)
[3] => (
[3] => test3u
[3] => test3d
)
[1] => (
[u] => test1u
[d] => test1d
)
[2] => (
[u] => test2u
[d] => test2d
)
[1] => (
[u] => test1u
[d] => test1d
)
)
it should only return array([1] => ( [u] => test1u [d] => test1d))
EDIT:
there are duplicate entries in the array because the array came from $arr = json_decode($arr); and the original JSON had duplicate entries.
if there is a better way to do this without decoding the json, let me know.
this is being used as part of a search program. the JSON is an array of all of the entries from the source array that met the criteria for one of the search terms. keeping the entries with the most duplicates insures that those entries contained most if not all of the search terms.
here is the JSON file being decoded:
[{"1":[{"u":"testing","d":"2017\/04\/27","st":"Test","i":"roses","v":"1","t":"org","sp":"N\/A","k":"0","img":"--"}]},{"2":[{"u":"testing","d":"2017\/04\/27","st":"Test","i":"roses","v":"1","t":"org","sp":"N\/A","k":"0","img":"--"}]},{"5":[{"u":"testing","d":"2017\/04\/27","st":"Test","i":"roses daffodil","v":"1","t":"org","sp":"N\/A","k":"0","img":"--"}]},{"3":[{"u":"testing","d":"2017\/04\/27","st":"Test","i":"daffodil","v":"1","t":"org","sp":"N\/A","k":"0","img":"--"}]},{"4":[{"u":"testing","d":"2017\/04\/27","st":"Test","i":"daffodil","v":"1","t":"org","sp":"N\/A","k":"0","img":"--"}]},{"5":[{"u":"testing","d":"2017\/04\/27","st":"Test","i":"roses daffodil","v":"1","t":"org","sp":"N\/A","k":"0","img":"--"}]},{"6":[{"u":"testing","d":"2017\/04\/27","st":"Test","i":"roses daffodil","v":"1","t":"org","sp":"N\/A","k":"0","img":"--"}]}]
in this case the search that made this JSON was for "roses daffodil"
the second example it add – each index just can appear once.
for the first this should work fine:
<?php
$array[] = array( 'u' => 'test1u', 'd' => 'test1d' );
$array[] = array( 'u' => 'test2u', 'd' => 'test2d' );
$array[] = array( '3' => 'test3u', '3' => 'test3d' );
$array[] = array( 'u' => 'test1u', 'd' => 'test1d' );
$array[] = array( 'u' => 'test2u', 'd' => 'test2d' );
var_export( $array );
//echo("\n" . array_count_values($array) . "\n");
foreach( $array as $k => $v ){
foreach( $array as $ke => $ve ){
if( $k == $ke )
continue;
if( $v == $ve ) {
$d[$k]=$v;
unset($array[$k]);
}
}
}
var_export( $d );
?>
unfortunately array_count_values only works for String and int, so it does not work when you have complex values.
First you array cannot have same key. Check the live demo.
<?php
$array[] = array( 'u' => 'test1u', 'd' => 'test1d' );
$array[] = array( 'u' => 'test2u', 'd' => 'test2d' );
$array[] = array( 'u' => 'test3u', 'd' => 'test3d' );
$array[] = array( 'u' => 'test1u', 'd' => 'test1d' );
$array[] = array( 'u' => 'test2u', 'd' => 'test2d' );
$array = array_map(function($v){return implode('-', $v);}, $array);
$count = array_count_values($array);
print_r($count);
arsort($count);
$max = current($count);
while(current($count) == $max)
{
$arr = explode('-', key($count));
$result[] = array('u' => $arr[0], 'd' => $arr[1]);
next($count);
}
print_r($result);
i have found the solution! bit long winded but it works!
$json = json_decode($json);
$jsonoutc = $jsonout = "";
$arrid = $arrout = $disp = array();
foreach ($json as $null => $arr){
foreach ($arr as $key => $null){
$arrid[] = $key;
}
}
$vals = array_count_values($arrid);
foreach ($vals as $val => $counted){
if ($counted > $jsonoutc){
$jsonoutc = $counted;
}
}
foreach ($vals as $val => $counted){
if ($counted == $jsonoutc){
$arrout[] = $val;
}
}
foreach ($arrout as $null => $val){
foreach ($json as $null => $arr){
foreach ($arr as $key => $list){
if ($key == $val){
$disp[$key] = $list;
}
}
}
}
print_r($disp);

PHP combine two arrays using key values

This question may sound silly, but I didn't find a good approach.
I have to multidimensional arrays like this:-
array
(
[0] => Array
(
[field1] => A
[field2] => 100
[field3] => 20
)
[1] => Array
(
[field1] => B
[field2] => 100
[field3] => 30
)
[2] => Array
(
[field1] => C
[field2] => 100
[field3] => 30
)
)
array
(
[0] => Array
(
[field1b] => A
[field2b] => 500
[field3b] => 0
)
[1] => Array
(
[field1b] => B
[field2b] => 300
[field3b] => 10
)
)
I want to merge both arrays using field1 as Key, and having in mind that arrays aren't same size.
The result array should be:
array
(
[0] => Array
(
[field1] => A
[field2] => 100
[field3] => 20
[field2b] => 500
[field3b] => 0
)
[1] => Array
(
[field1] => B
[field2] => 100
[field3] => 30
[field2b] => 300
[field3b] => 10
)
[2] => Array
(
[field1] => C
[field2] => 100
[field3] => 30
)
)
Thanks in advance!
Try this,
$array1 = array
(
"0" => Array
(
"field1" => "A",
"field2" => "100",
"field3" => "20"
),
"1" => Array
(
"field1" => "B",
"field2" => "100",
"field3" => "30"
),
"2" => Array
(
"field1" => "C",
"field2" => "100",
"field3" => "30"
)
);
$array2 = array
(
"0" => Array
(
"field1b" => "A",
"field2b" => "500",
"field3b" => "0"
),
"1" => Array
(
"field1b" => "B",
"field2b" => "300",
"field3b" => "10"
)
);
foreach($array1 as $key=>$val)
{
$new_array[$key]['field1'] = $val['field1'];
$new_array[$key]['field2'] = $val['field2'];
$new_array[$key]['field3'] = $val['field3'];
if (array_key_exists($key,$array2))//Check if the key is exists in an array
{
$new_array[$key]['field2b'] = $array2[$key]['field2b'];
$new_array[$key]['field3b'] = $array2[$key]['field3b'];
}
}
echo "<pre>";
print_r($new_array);
echo "</pre>";
DEMO
Very simple:- apply foreach and do it like below:-
<?php
$final_array = array();
foreach ($arr1 as $key=>$val){
if(isset($arr2[$key])){
$final_array[$key] = array_merge($val,$arr2[$key]);
unset($final_array[$key]['field1b']);
}else{
$final_array[$key] = $val;
}
}
echo "<pre/>";print_r($final_array);
Output:- https://eval.in/661803
If your both array have different number of elements then :-
<?php
$count1 = count ($arr1);
$count2 = count ($arr2);
$final_array = array();
if ($count1 >$count2){
foreach ($arr1 as $key=>$val){
if(isset($arr2[$key])){
$final_array[$key] = array_merge($val,$arr2[$key]);
unset($final_array[$key]['field1b']);
}else{
$final_array[$key] = $val;
}
}
echo "<pre/>";print_r($final_array);
}else if ($count2 >$count1){
foreach ($arr2 as $key=>$val){
if(isset($arr1[$key])){
$final_array[$key] = array_merge($val,$arr1[$key]);
unset($final_array[$key]['field1b']);
}else{
$final_array[$key] = $val;
}
}
echo "<pre/>";print_r($final_array);
}else{
foreach ($arr1 as $key=>$val){
if(isset($arr2[$key])){
$final_array[$key] = array_merge($val,$arr2[$key]);
unset($final_array[$key]['field1b']);
}else{
$final_array[$key] = $val;
}
}
echo "<pre/>";print_r($final_array);
}
<?php
$array1 = array
(
"0" => Array
(
"field1" => "A",
"field2" => "100",
"field3" => "20"
),
"1" => Array
(
"field1" => "B",
"field2" => "100",
"field3" => "30"
),
"2" => Array
(
"field1" => "C",
"field2" => "100",
"field3" => "30"
)
);
$array2 = array
(
"0" => Array
(
"field1b" => "A",
"field2b" => "500",
"field3b" => "0"
),
"1" => Array
(
"field1b" => "B",
"field2b" => "300",
"field3b" => "10"
)
);
$new_array1 = [];
foreach ($array1 as $object1) {
$new_array1[$object1['field1']] = $object1;
}
$new_array2 = [];
foreach ($array2 as $key => $object2) {
$new_array2[$object2['field1b']] = $object2;
}
$new_array = [];
foreach($new_array1 as $key => $val)
{
$new_array[$key]['field1'] = $val['field1'];
$new_array[$key]['field2'] = $val['field2'];
$new_array[$key]['field3'] = $val['field3'];
if (array_key_exists($key,$new_array2))//Check if the key is exists in an array
{
$new_array[$key]['field2b'] = $new_array2[$key]['field2b'];
$new_array[$key]['field3b'] = $new_array2[$key]['field3b'];
}
}
print_r($new_array);
DEMO
You can try it.
What about this instead defining hard-coded keys condition? all you need is to provide the key which should not be included in combined array..
Demo
<?php
$A = array(
array(
'field1' => 'A',
'field2' => '100',
'field3' => '20',
),
array(
'field1' => 'B',
'field2' => 100,
'field3' => 30,
),
array(
'field1' => 'C',
'field2' => 100,
'field3' => 30,
)
);
$B = array(
array(
'field1b' => 'A',
'field2b' => 500,
'field3b' => 0
),
array(
'field1b' => 'B',
'field2b' => 300,
'field3b' => 10
)
);
$combined = array();
foreach($A as $key => $arr) {
$tempKey = reset($arr);
$second_arr = $B[ $key ];
$tempKey2 = reset($B[ $key ]);
if( $tempKey == $tempKey2 ) {
$combined[] = $arr + $second_arr;
unset( $combined[ $key ]['field1b'] );
}
else {
$combined[] = $arr;
}
}
echo '<pre>';
print_r($combined);
echo '</pre>';

How to merge these 2 arrays in PHP?

I have an array $array1 with different amount of key and value pairs:
Array
(
[0] => Array
(
[ID] => 39
[title] => Apple
)
[1] => Array
(
[ID] => 40
[title] => Orange
)
)
and another array $array2:
Array
(
[0] => 273
[1] => 386
)
And I want to get this:
Array
(
[0] => Array
(
[ID] => 39
[title] => Apple
[pages] => 273
)
[1] => Array
(
[ID] => 40
[title] => Orange
[pages] => 386
)
)
The number of items in each array is the same and the correspond, so, we don't need to check this, so, how to merge it like that?
use array_replace_recursive if you want merge with integer keys, or array_merge_recursive if you want merge only string keys
<?php
$a1 = array(
0 => array
(
"ID" => 39,
"title" => "Apple"
),
1 => array(
"ID" => 40,
"title" => "Orange"
)
);
$a2 = array(
0 => array
(
"pages" => 273,
"year" => 1981
),
1 => array(
"pages" => 386,
"year" => 1979
)
);
$a3 = array_replace_recursive($a1, $a2);
var_dump($a3);
Result:
array(2) {
[0] =>
array(4) {
'ID' =>
int(39)
'title' =>
string(5) "Apple"
'pages' =>
int(273)
'year' =>
int(1981)
}
[1] =>
array(4) {
'ID' =>
int(40)
'title' =>
string(6) "Orange"
'pages' =>
int(386)
'year' =>
int(1979)
}
}
Answer for updated question:
<?php
$a1 = array(
0 => array
(
"ID" => 39,
"title" => "Apple"
),
1 => array(
"ID" => 40,
"title" => "Orange"
)
);
$a2 = array(
0 => 31,
1 => 324
);
$defaultValue = 0;
foreach ($a1 as $key => $value) {
$a1[$key]['pages'] = isset($a2[$key]) ? $a2[$key] : $defaultValue;
}
var_dump($a1);
Result:
array(2) {
[0] =>
array(3) {
'ID' =>
int(39)
'title' =>
string(5) "Apple"
'pages' =>
int(31)
}
[1] =>
array(3) {
'ID' =>
int(40)
'title' =>
string(6) "Orange"
'pages' =>
int(324)
}
}
Try this code :-
$newArray = array();
$i=0;
foreach($array1 as $value){
$newArray[$i] = $value;
$newArray[$i][] = $array2[$i];
$i++;
}
Here is code that produce exactly output you are looking for :
<?php
$list1 = array(array('id' => 39,'title' => 'Apple'),array('id' => 40,'title' => 'Orange'));
$list2 = array(array('pages' => 273,'year' => 1981),array('pages' => 386,'year' => 1979));
$newList = array();
$i=0;
foreach($list1 as $firstList){
foreach($list2 as $secondList){
$newList[$i] = array_merge($firstList, $secondList);
}
$i++;
}
echo"<pre>"; print_r($newList); echo"</pre>";
?>
OUTPUT :
Try this
array1[0][pages]=array2[0][pages];
array1[0][year]=array2[0][year];
array1[1][pages]=array2[1][pages];
array1[1][year]=array2[1][year];
for($i=0; $i<count($array1); ++$i){
$array1[$i]['pages'] = $array2[$i];
}
var_dump($array1);
$output = array();
array_walk( $array1, function( $v, $k ) use ( $array2, &$output ) {
$output[] = array_merge( $v, $array2[$k] );
});
If the sizes of the 2 arrays are same you could do something like this
$arr1 = array(
array(
'ID' => 39,
'title' => 'Apple'
),
array(
'ID' => 40,
'title' => 'Orange'
)
);
$arr2 = array(
273,
386
);
$merged = array();
for ($i = 0; $i < count($arr1); $i++) {
$merged[] = array_merge($arr1[$i], array('pages' => $arr2[$i]));
}
var_dump($merged);
or if you don't want a new array
for ($i = 0; $i < count($arr1); $i++) {
$arr1[$i]['pages'] = $arr2[$i];
}

count the times a certain key name shows up in a multi-array?

How may I count the times a key shows up in a multi-array?
In the below example, weight should return 2, and reps 4. Does PHP have a built-in function for this?
[0] => Array
(
[0] => Array
(
[weight] => 317.51474856007
[reps] => 10
)
[1] => Array
(
[weight] => 50
[reps] => 10
)
[2] => Array
(
[reps] => 10
)
[3] => Array
(
[reps] => 10
)
)
you can try something like this:
function counter($array){
$result = array();
foreach($array as $key=>$value){
foreach($value as $name => $val){
$result[$name][]=$val;
}
}
return array('all'=>$result,'count weight'=>count($result['weight']),'count reps'=>count($result['reps']));
}
$array= Array(
'0' => Array
(
'weight' => 317.51474856007,
'reps' => 10
),
'1' => Array
(
'weight' => 50,
'reps' => 10
),
'2' => Array
(
'reps' => 12
),
'3' => Array
(
'reps' => 10
)
);
$resp = counter($array);
var_dump($resp);
Try this one liner using array_walk_recursive():
Given your array, $arr
array_walk_recursive($arr, function($v, $k)use(&$count){if(array_key_exists($k, $count))$count[$k]++;else $count[$k]=1;},$count=array());
print_r($count);
See demo
or the old fashioned way:
$count = array();
foreach ($arr as $ar){
foreach ($ar as $k=>$v){
if (array_key_exists($k, $count)){
$count[$k]++;
}
else{
$count[$k] = 1;
}
}
}
print_r($count);
See demo
Output:
Array
(
[weight] => 2
[reps] => 4
)
you can try this:
$array= Array(
'0' => Array
(
'weight' => 317.51474856007,
'reps' => 10
),
'1' => Array
(
'weight' => 50,
'reps' => 10
),
'2' => Array
(
'reps' => 10
),
'3' => Array
(
'reps' => 10
)
);
$weight=0;
$reps=0;
//print_r($array);
foreach($array as $key=>$val){
if(isset($val['weight'])){
echo $val['weight']."<br>";
$weight++;
}
if(isset($val['reps'])){
echo $val['reps']."<br>";
$resp++;
}
}
echo $weight." ".$resp;

Mix arrays together

I have this PHP array:
Array(
[0] => Array(
["Import"]=> Array
(
[0] => 1.00
[1] => 2.00
[2] => 1.00
[3] => 9.00
)
["Page"] => Array
(
[0] => 1
[1] => 4
[2] => 5
[3] => 6
)
["Key"] => Array
(
[0] => 1
[1] => 22
[2] => 88
[3] => 3
)
)
)
I need to get:
Array(
[0] => Array(
[0] => Array(
["Import"] => 1.00
["Page"] => 1
["Key"] => 1
)
[1] => Array(
["Import"] => 2.00
["Page"] => 4
["Key"] => 22
)
[2] => Array(
["Import"] => 1.00
["Page"] => 5
["Key"] => 88
)
[3] => Array(
["Import"] => 9.00
["Page"] => 6
["Key"] => 3
)
)
)
I've checked array_merge and array_combine but I can't find a way to use them in this case.
How can I do?
Try this. Seems to work as you expect.
<?php
$source = array(
array(
'Imports' => array(
1.00,2.00,1.00, 9.00),
'Page' => array(
1,4,5,6),
'Key' => array(
1,22,88,3)
)
);
print_r($source);
$dest = array();
foreach($source as $key => $src) {
foreach($src as $typeKey => $typeArr) {
foreach($typeArr as $index => $val){
$dest[$key][$index][$typeKey] = $val;
}
}
}
print_r($dest);
?>
Here is a demo: http://codepad.org/bht4ne7K
In PHP 5.5 you can easily achieve that with array_column() function:
$array = [
'Imports' => ['i0', 'i1'],
'Page' => ['p0', 'p1'],
'Key' => ['k0', 'k1']
];
$i = 0;
$result = array_map(function($x) use ($array, &$i)
{
return array_combine(array_keys($array), array_column($array, $i++));
}, current($array));
//var_dump($result);
-but for earlier versions you'll need to gather your array with foreach like:
$result = [];
foreach($array as $key=>$item)
{
foreach($item as $i=>$value)
{
$result[$i][$key] = $value;
}
}
//var_dump($result);
-you'll need, of course, do that with each element of your parent array (code above is a sample of how to rearrange 2-level array). That, again, can be easily done with array_map()
Something like this:
function switchKeys(Array $input) {
$result = array();
foreach ($input as $field => $data) {
if (is_array($data)) {
foreach ($data as $index => $value) {
$result[$index][$field] = $value;
}
}
}
return $result;
}
$input = array(
"imports" => array(1.00, 2.00, 1.00, 9.00,),
"page" => array(1, 4, 5, 6,),
"key" => array(1, 22, 88, 3,),
);
$output = switchKeys($input);
var_export($input);
var_export($output);
Note that your input array has one more level, so you should call the function for each sub array

Categories