Give name of each index array - php

So I got a multidimensional array like this, it is a result of some calculation:
Array
(
[0] => Array
(
[0] => 0.0415
[1] => 0.083
[2] => 0.083
)
[1] => Array
(
[0] => 0.0325
[1] => 0.041
[2] => 0.025
)
[1] => Array
(
[0] => 0.076
[1] => 0.005
[2] => 0.031
)
)
Is it possible to give a name of each index? The result that I expected is like this:
Array
(
[A1] => Array
(
[C1] => 0.0415
[C2] => 0.083
[C3] => 0.083
)
[A2] => Array
(
[C1] => 0.0325
[C2] => 0.041
[C3] => 0.025
)
[A3] => Array
(
[C1] => 0.076
[C2] => 0.005
[C3] => 0.031
)
)

Yes it is possible with foreach loop.
$b=array();
foreach ($a as $k => $v) {
$new_key='A'.$k;
$new_array= array();
foreach ($v as $k2 => $val) {
$kkk = 'C'.$k2;
$new_array[$kkk]=$val;
}
$b[$new_key]=$new_array;
}

Related

php:array sum without foreach

I have an array like this
Array
(
[1_DAY_2017] => Array
(
[SAMSUNG] => Array
(
[0] => 549
[1] => 199
[2] => 999
)
[XIAOMI] => Array
(
[0] => 199
[1] => 2999
[2] => 499
)
)
[2_DAY_2017] => Array
(
[SAMSUNG] => Array
(
[0] => 699
[1] => 999
)
[LENOVO] => Array
(
[0] => 280
[1] => 2550
[2] => 849
)
)
[3_DAY_2017] => Array
(
[OPPO] => Array
(
[0] => 500
[1] => 599
)
[SAMSUNG] => Array
(
[0] => 799
)
)
[4_DAY_2017] => Array
(
[SAMSUNG] => Array
(
[0] => 1299
[1] => 499
[2] => 799
[3] => 2500
)
[OPPO] => Array
(
[0] => 299
[1] => 349
[2] => 499
)
)
[5_DAY_2017] => Array
(
[XIAOMI] => Array
(
[0] => 500
[1] => 270
[2] => 340
)
[VIVO] => Array
(
[0] => 4599
[1] => 299
)
)
[6_DAY_2017] => Array
(
[VIVO] => Array
(
[0] => 240
[1] => 1899
[2] => 759
[3] => 530
)
[OPPO] => Array
(
[0] => 999
)
)
[7_DAY_2017] => Array
(
[OPPO] => Array
(
[0] => 300
[1] => 252
[2] => 1290
)
[LENOVO] => Array
(
[0] => 570
[1] => 1300
[2] => 666
)
)
)
From this i want to get an array
$output= [SAMSUNG => 9341, XIAOMI => 4807]
Here each item contains sum of the items in nested array.
Currently my solution conatins more than 2 for each loops but is there any way to optimize this??
You can use array_sum, array_map and array_column functions to get the result
array_sum(array_map('array_sum', array_column($a, 'SAMSUNG')))
With single array_reduce function:
// $arr is your initial array
$result = array_reduce($arr, function($r, $v){
if (isset($v['SAMSUNG'])) $r['SAMSUNG'] += array_sum($v['SAMSUNG']);
if (isset($v['XIAOMI'])) $r['XIAOMI'] += array_sum($v['XIAOMI']);
return $r;
}, ['SAMSUNG' => 0, 'XIAOMI' => 0]);
$sumArray = array();
foreach ($YourArray as $k=>$subArray) {
foreach ($subArray as $id=>$value) {
$sumArray[$id]+=$value;
}
}
print_r($sumArray);

determine average value from an array column

consider below array:-
Array
(
[0] => Array
(
[0] => 99895
[1] => 35378
[2] => 0.01
)
[1] => Array
(
[0] => 99895
[1] => 813
[2] => -0.97
)
[2] => Array
(
[0] => 99895
[1] => 771
[2] => 0.29
)
[3] => Array
(
[0] => 442
[1] => 833
[2] => -1.06
)
[4] => Array
(
[0] => 442
[1] => 485
[2] => -0.61
)
[5] => Array
(
[0] => 442
[1] => 367
[2] => -0.14
)
[6] => Array
(
[0] => 442
[1] => 478
[2] => 0.77
)
[7] => Array
(
[0] => 442
[1] => 947
[2] => -0.07
)
[8] => Array
(
[0] => 7977
[1] => 987
[2] => 0.76
)
[9] => Array
(
[0] => 7977
[1] => 819
[2] => 0.37
)
[10] => Array
(
[0] => 7977
[1] => 819
[2] => 0.36
)
[11] => Array
(
[0] => 7977
[1] => 653
[2] => 1.16
)
[12] => Array
(
[0] => 7977
[1] => 1653
[2] => 1.15
)
)
from the above array how will I determine the below array?
array
(
99895 => -0.223
442 => -0.22
7977 => 0.76
)
Actually I need the average value of column 3 in respect of column 1.
First collect all the column 3 elements into an array keyed off column 1:
$arrays = array();
foreach ($input as $vals) {
$key = $vals[0];
$val = $vals[2];
if (isset($arrays[$key])) {
$arrays[$key][] = $val;
} else {
$arrays[$key] = array($val);
}
}
Now go through all of them, calculating the averages:
foreach ($arrays as &$array) {
$array = array_sum($array)/count($array);
}

array search for a key=>value

I have an array,
$arr=(
[0] => Array
(
[groupid] => 1
[groupname] => Red
[members] => Array
(
[0] => Array
(
[mid] => 9
[name] => Anith
)
[1] => Array
(
[mid] => 11
[name] => Aravind
)
[2] => Array
(
[mid] => 10
[name] => Lekshmi
)
)
)
[1] => Array
(
[groupid] => 2
[groupname] => Blue
[members] => Array
(
[0] => Array
(
[mid] => 6
[name] => Yamuna
)
[1] => Array
(
[mid] => 2
[name] => Kamala K
)
[2] => Array
(
[mid] => 13
[name] => Sooraj K
)
)
)
I want to check [mid] => 2 is in the array..If it exists
I want to delete it(ie. unset the array )-----
[1] => Array
(
[mid] => 2
[name] => Kamala K
)
;;;
eg:--unset($arr[1]['members'][2];
This should do the trick
foreach ($arr as $group => $subarray) {
foreach ($subarray['members'] as $k => $v) {
if ($v['mid'] == 2) {
unset($arr[$group]['members'][$k]);
break;
}
}
}
var_dump($arr);
If you feel like getting crafty, you could do something like this:
// note: requires PHP >= 5.3
foreach ($arr as $key => &$value) {
$value['members'] = array_filter(
$value['members'],
function($member) {
return $member['mid'] != 2;
}
);
}
var_dump($arr);

How to combine arrays in php?

I have these arrays:
$array1
Array
(
[0] => Array
(
[state] => AE
[state_pri] => 0
)
[1] => Array
(
[state] => AK
[state_pri] => 0
)
[2] => Array
(
[state] => AL
[state_pri] => 0
)
)
$array2
Array
(
[0] => Array
(
[0] => Array
(
[count1] => 0
)
)
[1] => Array
(
[0] => Array
(
[count1] => 1
)
)
[2] => Array
(
[0] => Array
(
[count1] => 18
)
)
)
$array3
Array
(
[0] => Array
(
[0] => Array
(
[count] => 0
)
)
[1] => Array
(
[0] => Array
(
[count] => 1
)
)
[2] => Array
(
[0] => Array
(
[count] => 18
)
)
)
...and I would like to get something like this:
Array
(
[0] => Array
(
[state] => AE
[state_pri] => 0
[0] => Array
(
[count] => 0
)
[0] => Array
(
[count1] => 0
)
)
[1] => Array
(
[state] => AK
[state_pri] => 0
[1] => Array
(
[count] => 0
)
[1] => Array
(
[count1] => 0
)
)
[2] => Array
(
[state] => AL
[state_pri] => 0
[2] => Array
(
[count] => 0
)
[2] => Array
(
[count1] => 0
)
)
)
Any ideas on how to do this?
Edit: Just to add some more code, if I use array_merge I get:
Array
(
[0] => Array
(
[state] => AE
[state_pri] => 0
)
[1] => Array
(
[state] => AK
[state_pri] => 0
)
[2] => Array
(
[state] => AL
[state_pri] => 0
)
[3] => Array
(
[0] => Array
(
[count] => 0
)
)
[4] => Array
(
[0] => Array
(
[count] => 1
)
)
[5] => Array
(
[0] => Array
(
[count] => 18
)
)
.....
)
What you're looking for is still a bit difficult to make out. Let me know if this achieves the desired results:
foreach ($array1 as $key => $val) {
$array1[$key][] = $array3[$key];
$array1[$key][] = $array2[$key];
}
print_r($array1);
What about the array_merge function?
http://www.php.net/manual/en/function.array-merge.php
$merged = array();
foreach (array($array1,$array2,$array3) as $array) {
foreach ($array as $key=>$value) {
if (!isset($merged[$key])) {
$merged[$key] = array();
}
$merged[$key] += $value;
}
}
print_r($merged);
$combined=$array0 + $array1;
Try that one

array difference

I have this array lets call it array 1
Array
(
[0] => Array
(
[Machine] => Array
(
[id] => 7
[name] => XYZ
[priority] => 1
)
[Software] => Array
(
[id] => 472
)
)
[1] => Array
(
[Machine] => Array
(
[id] => 6
[name] => ABC
[priority] => 0
)
[Software] => Array
(
[id] => 470
)
)
[2] => Array
(
[Machine] => Array
(
[id] => 1
[name] => IEU
[priority] => 3
)
[Software] => Array
(
[id] => 471
)
)
)
Then I have another array lets call it array 2
Array
(
[0] => 7
[1] => 5
[2] => 4
[3] => 3
[4] => 6
)
If array 2 doesnt have [Machine][id] then I want it to be removed from array 1. Like in above example 1 will removed
[2] => Array
(
[Machine] => Array
(
[id] => 1
[name] => IEU
[priority] => 3
)
[Software] => Array
(
[id] => 471
)
)
any idea on how to achieve that. Thanks
Perhaps..
foreach ($array1 AS $key => $array) {
if (!in_array($array['Machine']['id'], $array2))
unset($array1[$key]);
}
try something like :
$new_array = array();
foreach ($array1 as $platform)
{
if (in_array($platform["Machine"]["id"], $array2))
{
$new_array[] = $platform;
}
}
return $new_array;

Categories