How to sum array values based on keys? - php

The first array
Array
(
[0] => Array
(
[1] => 2
)
[1] => Array
(
[1] => 2
)
[2] => Array
(
[2] => 1
)
[3] => Array
(
[3] => 1
)
)
I want output like
Array
(
[0] => Array
(
[1] => 4
)
[1] => Array
(
[2] => 1
)
[2] => Array
(
[3] => 1
)
)
How can i do this?

Seems like a good case for array_reduce():
$res = array_chunk(array_reduce($arr, function(&$current, $item) {
foreach ($item as $key => $value) {
if (!isset($current[$key])) {
$current[$key] = 0;
}
$current[$key] += $value;
}
return $current;
}, []), 1, true);
For the final result I'm using array_chunk(); it takes an array and creates single element sub arrays of each element.

$result = array();
foreach ($input as $subarray) {
foreach ($subarray as $key => $value) {
if (isset($result[$key])) {
$result[$key][$key] += $value;
} else {
$result[$key] = array($key => $value);
}
}
}
$result = array_values($result); // Convert from associative array to indexed array

Related

How can I retrieve data from array

Am a beginner in yii2. In my program there is an array named Place. It contains the values like:
Array
(
[0] => Array
(
[0] => Aluva
[1] => Paravoor
)
[1] => Array
(
[0] => Ernakulam
[1] => Paravoor
)
[2] => Array
(
[0] => Aluva
[1] => Ernakulam
)
[3] => Array
(
[0] => Kottuvally
[1] => Paravoor
)
)
How can I retrieve each element from this array?
You can use the array_walk function
$total = [];
array_walk($sales, function($value) use(&$total) {
foreach ($value as $key => $arr) {
echo $arr. "<br>";
}
});
using foreach ?
foreach($array as $row){
foreach($row as $key => $val){
echo $val.'<br>';
}
}
Sample code -
<?php
$data = array(
0 => array(0=>'Aluva', 1=>'Paravoor'),
1 => array(0=>'Ernakulam', 1=>'Paravoor'),
2 => array(0=>'Aluva', 1=>'Ernakulam'),
3 => array(0=>'Kottuvally', 1=>'Paravoor')
);
foreach ($data as $key => $value) {
foreach ($value as $key1 => $value1) {
echo '<br>'.$value1;
}
}
$var = $Place[0][1];
Where is $var is now "Paravoor"

How to Sort all data from array

how can i crawl this array with a for loop or foreach ,
this is the content of a single array:
Array ( [0] => 1 ) Array ( [0] => 1 [1] => 2 ) Array ( [0] => 1 [1] => 2 [2] => 3 )
If you mean what I think you mean...
try this:
$tempArray = array();
foreach($myarray as $k => $v){
foreach($v as $ke => $val ){
$tempArray[] = $val;
}
}
sort($tempArray);
Or if you want to avoid duplicates try:
$temp = array_unique($myArray);
sort($temp);

how to intersect 2 multidimensional array into third multimensional array

i have 2 arryas and i want them to intersect and store the finding matches into third array with values from first array and second array.
the first array looks like this:
Array
(
[0] => Array
(
[0] => 45
[1] => 10640
[2] => 1041-0567041700116
)
[1] => Array
(
[0] => 46
[1] => 10640
[2] => 1041-0567041700318
)
[2] => Array
(
[0] => 207
[1] => 10645
[2] => 03320103000052
)
and the second array:
Array
(
[0] => Array
(
[0] => 03320103000052
[1] => 0
)
[1] => Array
(
[0] => 10013800805001
[1] => 12
)
[2] => Array
(
[0] => 1090-0360141758201
[1] => 3
)
the out put should be:
Array
(
[0] => Array
(
[0] => 207 =>value from first array
[1] => 10645 =>value from first array
[2] => 03320103000052 =>value from first and second array (this is what i need to compare)
[3] => 0 =>value from second array
)
this is similar to this post
but i have problems to store data into multidimensional array
thanks in forward for any suggestions and help
You can do this with only two foreach loops and one if statement:
$combined = array();
foreach ($array1 as $a) {
foreach ($array2 as $b) {
if ($a[2] == $b[0]) {
$combined[] = array($a[0], $a[1], $a[2], $b[1]);
}
}
}
The following is the test I set up to try this:
<?php
$array1 = array();
$array1[] = array('45', '10640', '1041-0567041700116');
$array1[] = array('46', '10640', '1041-0567041700318');
$array1[] = array('207', '10645', '03320103000052');
$array2 = array();
$array2[] = array('03320103000052', '0');
$array2[] = array('10013800805001', '12');
$array2[] = array('1090-0360141758201', '3');
$combined = array();
foreach ($array1 as $a) {
foreach ($array2 as $b) {
if ($a[2] == $b[0]) {
$combined[] = array($a[0], $a[1], $a[2], $b[1]);
}
}
}
print_r($combined);
?>

how to get array from get to normal array in php

I have an array like this and it can contain multiple values:
Array
(
[rpiid] => Array
(
[1] => 86
)
[sensor_id] => Array
(
[1] => 1
)
[when] => Array
(
[1] => 2014-02-24
)
[val] => Array
(
[1] => 000
)
[train] => Array
(
[1] => True
)
[valid] => Array
(
[1] => False
)
[button] => update
)
Of course, here there is only the number 1 each time but sometimes I have 0, 1, 2 and a value associated. This is because I get this from a GET from multiple forms.
How can I transform this array into
Array
(
[0] => Array
(
[rpiid] => 86
[sensor_id] => 1
...
Thanks,
John.
if your array is $get
$newArray = Array();
foreach($get as $secondKey => $innerArray){
foreach($value as $topKey => $value) {
$newArray[$topKey][$secondKey] = $value;
}
}
This should work
$new_array = array();
foreach($first_array as $value => $key){
$new_array[$key] = $value[1];
}
Sure you can, take a look at this small example:
$a = [ 'rpid' => [1], 'cpid' => [2,2] ];
$nodes = [];
foreach($a as $node => $array) {
foreach($array as $index => $value) {
if(empty($nodes[$index]))
$nodes[$index] = [];
$nodes[$index][$node] = $value;
}
}
print_r($nodes):
Array
(
[0] => Array
(
[rpid] => 1
[cpid] => 2
)
[1] => Array
(
[cpid] => 2
)
)

extract values from multidemensional array

I have two arrays that are structured like this
$array1 = Array
(
[0] => Array
(
['story_id'] => 47789
)
[1] => Array
(
['story_id'] => 47779
)
[2] => Array
(
['story_id'] => 47776
)
[3] => Array
(
['story_id'] => 47773
)
[4] => Array
(
['story_id'] => 47763
)
)
$array2 = Array
(
[0] => Array
(
['story_id'] => 47789
)
[1] => Array
(
['story_id'] => 47777
)
[2] => Array
(
['story_id'] => 47776
)
[3] => Array
(
['story_id'] => 47773
)
[4] => Array
(
['story_id'] => 47763
)
)
and I want to get the difference of array1 from array2 so I tried using
$results = array_diff($array1, $array2);
but it turns up empty is there any easy way around this or would it be best for me to get the arrays boiled down and if so is there easy way to do that ?
It because that the array_diff is only use for 1 dimension array. For your 2 array, let use some code from php.net
function multidimensional_array_diff($a1, $a2)
{
$r = array();
foreach ($a2 as $key => $second) {
foreach ($a1 as $key => $first) {
if (isset($a2[$key])) {
foreach ($first as $first_value) {
foreach ($second as $second_value) {
if ($first_value == $second_value) {
$true = true;
break;
}
}
if (!isset($true)) {
$r[$key][] = $first_value;
}
unset($true);
}
} else {
$r[$key] = $first;
}
}
}
return $r;
}

Categories