Array (
[1] => Array (
[poidsTot] => 1
[idBout] => 1
)
[2] => Array (
[poidsTot] => 2
[idBout] => 1
)
[3] => Array (
[poidsTot] => 2
[idBout] => 2
)
[4] => Array (
[poidsTot] => 8
[idBout] => 2
)
)
This is a table containing several arrays with two values each. The value1 is a weight and the value2 is an identifier. I want to sum all the weights with the same identifier. help. thank you in advance!
$newarr is a new array of sum
$newarr = array();
foreach($a as $onea)
{
$newarr[$onea['idBout']]+= $onea['poidsTot'];
}
which results in:
Array
(
[1] => 3
[2] => 10
)
https://eval.in/776652
Related
I would like to sum up several arrays. I have a script that dynamically creates arrays without name. Below is an example. In my script i have a 90+ arrays. I want to sum up all it. All keys in that arrays be user id, so i only want sum values of keys. How to do it? Regards
Array
(
[1] => 1
[2] => 1
[3] => 1
)
Array
(
[1] => 1
[2] => 1
)
Array
(
[1] => 1
)
I want to get only one array result like:
Array
(
[1] => 3
[2] => 2
[3] => 1
)
Add all your arrays into one array.
$all_arrays[] =Array
(
[1] => 1 ,
[2] => 1,
[3] => 1
);
$all_arrays[] = Array
(
[1] => 1,
[2] => 1
) ;
$all_arrays[] = Array
(
[1] => 1
);
$results = [];
foreach($all_arrays as $arr){
foreach($arr as $user_id=>$value){
if(in_array($user_id,$results)){
$results[$user_id] = $results[$user_id] + $value;
}else{
$results[$user_id] = $value;
}
}
}
You would need to specify all arrays in the array_merge:
$result = array_count_values(array_keys(array_merge($array1, $array2, $array3)));
If you can get the arrays dynamically added to another $main_array like:
Array
(
[0] => Array
(
[1] => 1
[2] => 1
[3] => 1
)
[1] => Array
(
[1] => 1
[2] => 1
)
[2] => Array
(
[1] => 1
)
)
Then it would be much easier:
$result = array_count_values(array_keys(array_merge(...$main_array)));
Im kind of stuck since I can't figure out how to solve this problem. I can't seem to find the exact solution on the internet so that's why I am asking it here.
Example:
# array1
Array
(
[0] => Array
(
[0] => Product1
[1] => Description product 1
)
[1] => Array
(
[0] => Product2
[1] => Description product 2
)
[2] => Array
(
[0] => Product3
[1] => Description product 3
)
)
# array2
Array
(
[0] => 10
[1] => 20
[2] => 30
)
#resultant array
Array
(
[0] => Array
(
[0] => Product1
[1] => Description product 1
[2] => 10
)
[1] => Array
(
[0] => Product2
[1] => Description product 2
[2] => 20
)
[2] => Array
(
[0] => Product3
[1] => Description product 3
[2] => 30
)
)
I am programming in PHP, using no frameworks. I would like some help to find something that can result in #resultant array.
I have tried using the build in PHP functions array_merge();. But that doesn't work. I am guessing I need some kind of foreach or loop but I can't figure out how to build/write that.
Thanks for reading, I hope to find a solution or a lead on where to start.
Just loop array2 and add the value to array1.
foreach($arr2 as $key => $val){
$arr1[$key][] = $val;
}
Please try to do like this
$a = array(
'0' => array(
'0' => 1,
'1' => 2
),
'1' => array(
'0' => 3,
'1' => 4
),
);
$b = array(
'0' => 10,
'1' => 20
);
$c = $a;
foreach ($c as $key => $value) {
array_push($c[$key], $b[$key]);
}
print_r($c);
I don't really know how to explain what I would like to do, so here is an example. I have an 2D array, like this one :
Array
(
[0] => Array
(
[1] => value 1
[2] => value 2
)
[1] => Array
(
[1] => value 1
[2] => value 2
)
[2] => Array
(
[1] => value 1
[2] => value 2
)
)
And I would like to have this :
Array
(
[0] => Array
(
[1] => value 1
[2] => value 2
[3] => value 3
)
[1] => Array
(
[1] => value 1
[2] => value 2
[3] => value 3
)
[2] => Array
(
[1] => value 1
[2] => value 2
[3] => value 3
)
)
Can someone help me ? Thanks very much.
Just loop it and make sure to reference & the $val to update the original array. Then just append the new item:
foreach($array as &$val) {
$val[] = 'value 3';
}
The other way:
foreach($array as $key => $val) {
$array[$key][] = 'value 3';
}
This is my array
Array
(
[2] => Array
(
[0] => Array
(
[id] => 2
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:38 AM
[status] => 0
)
[1] => Array
(
[id] => 2
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:38 AM
[status] => 0
)
)
[1] => Array
(
[0] => Array
(
[id] => 1
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:17 AM
[status] => 0
)
)
)
From this I need sub array count i.e., the array having two indexes such as 2 & 1 from this 2 & 1 there are some nested arrays found such as 0 & 1 for each
Here,I need array count as follows
Array
(
[2] => Array
(
[count] = 2
)
[1] => Array
(
[count] = 1
)
)
How should I get this..
Someone help me out of this...
Thank you..
It is very easy foreach your array and use count or sizeof function.
$desiredArray = array();
foreach ($myarray as $key => $value) {
$desiredArray [$key] ['count'] = sizeof ($value);
}
print_r ($desiredArray);
The output will be as your desired output
Array
(
[2] => Array
(
[count] = 2
)
[1] => Array
(
[count] = 1
)
)
It's simple, and is better to create new array where you can save count of elements of main array items:
$counts = array();
foreach ($array as $k => $values) {
$counts[$k] = count($values);
}
print($counts); // gives desired result
Also you don't need to have extra array for the $counts array, what you get is:
array (
2 => 2,
1 => 1
)
I have following arrays:
1) for total placed
Array
(
[0] => Array
(
[centers] => Array
(
[name] => delhi
[id] => 1
)
[0] => Array
(
[totalplaced] => 8
)
)
[1] => Array
(
[centers] => Array
(
[name] => mumbai
[id] => 2
)
[0] => Array
(
[totalplaced] => 1
)
)
)
2) for total working
Array
(
[0] => Array
(
[centers] => Array
(
[name] => delhi
[id] => 1
)
[0] => Array
(
[totalworking] => 4
)
)
[1] => Array
(
[centers] => Array
(
[name] => mumbai
[id] => 2
)
[0] => Array
(
[totalworking] => 1
)
)
)
3) for total trained
Array
(
[0] => Array
(
[centers] => Array
(
[name] => delhi
[id] => 1
)
[0] => Array
(
[totaltrained] => 8
)
)
[1] => Array
(
[centers] => Array
(
[name] => mumbai
[id] => 2
)
[0] => Array
(
[totaltrained] => 1
)
)
)
I wanted to merge these arrays so that the resultant array should look like this
[newarray] => Array(
[0] => Array (
[centers] => Array
(
[name] => delhi
[id] => 1
[totalplaced] => 8
[totalworking] => 4
[totaltrained] => 8
)
)
[1]=> Array(
[centers] => Array
(
[name] => mumbai
[id] => 2
[totalplaced] => 1
[totalworking] => 1
[totaltrained] => 1
)
)
)
This is the tabular representation of the above data which i want to display
centername totalplaced totalworking totaltrained
delhi 8 4 8
mumbai 1 1 1
Please help me on this.
Thanks
Pankaj Khurana
The difficulty here is that PHP's functions such as array_merge() and array_merge_recursive() will not merge data into numeric keys, but rather will re-key any duplicate numeric key. So for example given two arrays:
array(
'test' => 'abc',
0 => 'xyz'
);
array(
'test' => 'def',
0 => 'uvw'
);
Merging them together with array_merge() will produce an array like:
array(
'test' => 'def',
0 => 'xyz',
1 => 'uvw'
);
So, you need a custom function to be "additive" on any key, regardless of whether it is a string or numeric key. Try this:
function mixed_key_array_merge() {
$args = func_get_args();
$result = array();
foreach ($args as $arg) {
// discard non-array arguments; maybe this could be better handled
if (!is_array($arg)) {
continue;
}
foreach ($arg as $key => $value) {
if (!isset($result[$key])) {
$result[$key] = $value;
} else if (is_array($result[$key])) {
$result[$key] = call_user_func_array('mixed_key_array_merge',array($result[$key],$value));
}
}
}
return $result;
}