Sum the individual columns of 3 different arrays
I need to sum the arrays of individual columns
$a = [1,2,3,4];
$b = [5,6,8,7];
$c = [10,11,4,70];
the output will be like that..
$d = [(1+5+10),(2+6+11),(3+8+4),(4+7+70)];
then $d will be
$d = [16,19,15,81];
Here is the code:
$a = [1,2,3,4];
$b = [5,6,8,7];
$c = [10,11,4,70];
$limit = count($a);
$d = array();
for($i=0;$i<$limit;$i++){
$d[] = $a[$i]+$b[$i]+$c[$i];
}
var_dump($d);//array(16,19,15,81)
I hope it helps
$a = [1,2,3,4];
$b = [5,6,8,7];
$c = [10,11,4,70];
$res = [];
for($i=0;$i<sizeof($a);$i++)
{
$res[$i]=$a[$i]+$b[$i]+$c[$i];
}
You can try like this
$a = [1,2,3,4];
$b = [5,6,8,7];
$c = [10,11,4,70];
$myArray = array($a, $b, $c);
$sumArray = array();
foreach ($myArray as $k=>$subArray) {
foreach ($subArray as $id=>$value) {
$sumArray[$id]+=$value;
}
}
print_r($sumArray);
Related
$a = ['Ava', 'Emma', 'Olivia']; $b = ['Olivia', 'Sophia', 'Emma'];
I want the output to be ['Emma', 'Olivia', 'Ava', 'Sophia'] in any particular order without using array functions.
This is what i tried
<?php
//function unique_names($a,$b){
$a = ['Ava', 'Emma', 'Olivia'];
$b = ['Olivia', 'Sophia', 'Emma'];
$z= $a;
$c = count($b);
$d = count($a);
//loop for b
$e = 0;
for($i=0;$i<$c;$i++){ //b
for($j=0;$j<$d;$j++){
if($b[$i] != $a[$j]){
$z[$d+1] = $b[$i];
break;
}else{
//$z[$e] = $a[$j];
}
}
}
echo"<pre>ans";print_r($z);
die;
//return $z;
//}
//echo"<pre>ans"; print_r(unique_names($a,$b));
?>
Also i made it work using in_array but was later told that even that function is not allowed.
<?php
function unique_names($a,$b){
$z= $a;
$c = count($b);
$d = count($a);
for($i=0;$i<$c;$i++){
if(! in_array($b[$i], $a)){
$z[$d+1] = $b[$i];
}
}
return $z;
}
$a = ['Ava', 'Emma', 'Olivia'];
$b = ['Olivia', 'Sophia', 'Emma'];
print_r(unique_names($a,$b));
?>
You can use next code:
<?php
$a = ['Ava', 'Emma', 'Olivia'];
$b = ['Olivia', 'Sophia', 'Emma'];
$values = [];
// Add values from first array
foreach($a as $v) {
$values[$v] = true;
}
// Add values from second array
// all exists names will be overwrited
// new values will be addded
foreach($b as $v) {
$values[$v] = true;
}
// Transform keys to plain result
foreach($values as $key=>$val) {
$result[] = $key;
}
var_dump($result);
Execute PHP online
I had a array of list like this :
A = (a.11, b.12, c.dd)
I want to store the above array values in two different arrays like
B = (a, b, c)
C = (11,12,dd)
I tried a lot but all in vain. i am bit new to php. please help me out in this regard. Your prompt response is highly appreciated
Thanx
Hope this will help you:
$a = array("a.11,b.12,c.dd");
$b = array();
$d = array();
foreach ($a as $val)
{
$c =explode(',', $val);
foreach ($c as $v)
{
$e =explode('.', $v);
array_push($b,$e[0]);
array_push($d,$e[1]);
}
}
print_r($b);
print_r($d);
Working demo
foreach($A as $v) {
$v = explode('.', $v);
$B[] = $v[0];
$C[] = $v[1]
}
Try this,
$C = [];
$B = array_map(function($v) use(&$C){$arr = explode('.', $v); $C[] = $v[1]; return $v[0];}, $A);
I have a array which structure is
<?php
$a = [1,2,3,4,5,6];
$b = [];
?>
I want to add indexes of variable $a one by one to variable $b.
$a = [1,2,3,4,5,6];
$b = array_keys($a);
$b = array_values($a);
or you can do as follows :
foreach ($a as $v){
array_push($b, $v);
}
I guess you are looking for something like this,
$a = [1,2,3,4,5,6];
foreach ($a as $key => $value){
$b[] = $key;
}
<?php
foreach (array(1, 2, 3, 4) as &$value) {
$value = $value * 2;
}
?>
From what I understand from your question
<?php
$a = [1,2,3,4,5,6];
$b = array();
for ($i=0; $i < count($a) ; $i++) {
array_push($b, $i);
}
print_r($b);
?>
foreach ($a as $v){
$b[] = $v;
}
Or if you just want to copy the array, you can use array_merge instead:
$b = array_merge(array(), $a);
I misunderstood the question a bit. If you want to copy the keys and not the value you could do like this with foreach-loop:
foreach ($a as $k=>$v){
$b[] = $k;
}
Other suggestion like array_keys would work as well.
let me start off by saying what I have works just fine. However, I was wondering if there was a method using foreach loops, instead of while loops, that will return the same result. My current code is:
$a[0] = $_GET['a1'];
$a[1] = $_GET['a2'];
$a[2] = $_GET['a3'];
$a[3] = $_GET['a4'];
$a[4] = $_GET['a5'];
$a[5] = $_GET['a6'];
$a[6] = $_GET['a7'];
$a[7] = $_GET['a8'];
$a[8] = $_GET['a9'];
$a[9] = $_GET['a10'];
$a[10] = $_GET['a11'];
$a[11] = $_GET['a12'];
$a[12] = $_GET['a13'];
$a[13] = $_GET['a14'];
$a[14] = $_GET['a15'];
$a[15] = $_GET['a16'];
$a[16] = $_GET['a17'];
$a[17] = $_GET['a18'];
$a[18] = $_GET['a19'];
$a[19] = $_GET['a20'];
$a[20] = $_GET['a21'];
$a[21] = $_GET['a22'];
$a[22] = $_GET['a23'];
$a[23] = $_GET['a24'];
$a[24] = $_GET['a25'];
$a[25] = $_GET['a26'];
$a[26] = $_GET['a27'];
$a[27] = $_GET['a28'];
$array1 = array(
array(-0.0165333833412173,-0.805148897111146,-0.253228140499216,-0.184436610496828,-0.182460815146422,-0.562680616467753),
array(0.0530980021095432,0.197367160352892,0.486754591654999,0.100148472516492,0.129649075750223,-0.0891313538023273),
array(1.20820560236013,1.60297691019929,1.88645801788488,1.4097784563,1.35491780171462,1.83839262576814),
array(0.35474704442591,0.669288291278341,-0.618871707987998,0.314393917137463,0.369309934864932,0.302877707428276),
array(1.73737846681824,1.23045039205127,1.74985170545799,2.53444768504332,0.947425891979588,2.49693773110446),
array(1.08515864048978,1.19974869194067,1.40391203998614,0.645993089768699,1.18500365352365,0.183710871687018),
array(-0.158201130751605,0.245894811416122,-0.13491139550657,0.268781009345758,0.584094496717681,-0.475038989317082),
array(2.69120997158388,2.6335032877555,2.1678974119528,1.98159587601917,2.47871441914596,3.11218576826585),
array(1.58301428618536,1.4690581543491,1.64181831092486,1.31270093574087,0.956030437566211,1.68095459548238),
array(0.567053945109392,0.672686283651576,0.50520628014895,0.573859412747649,0.906581004455213,0.675935843127413),
array(2.21154924140175,2.4629227185282,2.44302564998387,1.76594656224375,2.24292394905905,2.46555856986067),
array(0.789058773638184,0.56494483459565,1.02220848055446,0.740827710977414,0.0568788365193514,0.974084436394248),
array(-1.22527151151768,-0.37116067796664,-0.993915606653743,-0.265414040283105,-0.0483919594705323,-1.26478189171631),
array(1.93198111668015,0.978691339072353,0.896606685235391,1.43344220993764,1.44428119515243,0.832846974841109),
array(0.0960968892129648,0.179429676667834,0.653561055735465,0.611111687937286,0.420917607921928,0.788124847059183),
array(-0.570188484564322,-0.367869613869776,-0.425708923999659,0.58428081962228,0.431696028180505,-0.560777966949268),
array(2.18753554884398,2.10453220450978,2.18396016604212,1.53663043726462,1.6128493800773,2.47023229651768),
array(0.933680338305853,0.701683572817621,1.05817969240128,1.13326689801564,1.06305023491714,1.28172788495342),
array(0.237030103809889,1.19429662809383,1.32352397170755,0.787441428409051,1.06861486730769,0.196323518345157),
array(1.27102015175144,0.999617071482296,1.14337292692749,1.79352497701309,1.14261585172827,1.18874166503307),
array(0.746730730970985,0.584005303699165,0.00845233248507581,0.617844244971367,0.646839413026708,0.393004935019083),
array(1.33877796293032,1.18403793988876,1.16686497194317,1.18487924665819,0.796233877768925,1.21668080067963),
array(1.66146357909026,1.52622605987311,1.51673796371559,2.45659237209795,1.58826974938031,1.87667392945756),
array(1.23205756178945,1.25461474916892,1.0738857243227,1.21949513612512,0.847503017550969,1.33075294766838),
array(0.976236852070675,1.0676137689421,1.08390543200419,1.19114160706157,0.804407032221587,0.918305581716723),
array(1.0514345299356,1.24825633668808,1.15750685444102,1.34538988886631,1.24490007119083,0.994386901437322),
array(-50.5445732202499,-53.6034355984409,-52.2301074110166,-59.4416059910645,-50.3559798265968,-51.5135149950383),
);
$array2 = array();
$x = 0;
while($x < COUNT($array1)){
$y = 0;
while($y < COUNT($array1[$x])){
$array2[$x][$y] = $array1[$x][$y] * $a[$x];
//echo($array2[$x][$y] . ', ');
$y++;
}
//echo('<br>');
$x++;
}
Again, while this works, I am trying to get into the habit of writing cleaner code. Additionally, I am still relatively new to PHP, so I'm still struggling a bit with some of its syntax and structuring.
Thanks in advance.
This should work identical to what you have.
$array2 = array();
foreach($a as $k1=>$a1)
{
foreach($array1[$k1] as $k2=>$a2)
{
$array2[$k1][$k2] = $a2 * $a1;
}
}
The basic format of a foreach loop is foreach($array as $key=>$value) if you don't need the array key, you can shorten it to foreach($array as $value).
You can reduce the code by directly accessing the $_GET values instead of copying them into $a.
$result = array();
foreach($array1 as $k1 => $array2) {
foreach($array2 as $k2 => $value) {
$result[$k1][$k2] = $value * $_GET['a'.($k1+1)];
}
}
So i'm working on a small php applications that combines four arrays.
Now there is a possibility that some of the possible arrays will be null.
I tried the following solution to merge the four arrays uniquely.
<?php
$a = [1,2,3,4,5];
$b = null;
$c = [5,4,3,2,1];
$d = [1,2];
$new_array;
if(is_array($a) && is_array($b) && is_array($c) && is_array($d))
{
$new_array = array_unique(array_merge($a,$b,$c,$d));
}else if(is_array($a) && is_array($b) && is_array($c))
{
$new_array = array_unique(array_merge($a,$b,$c));
}else if(is_array($a) && is_array($b))
{
$new_array = array_unique(array_merge($a,$b));
}else{
$new_array = $a;
}
print_r($new_array);
?>
I soon realized my code was highly dysfunctional in that it does not cater for all possible combinations of arrays while excluding the null variables.
How do I solve this. Ensuring that all the variables that are arrays are merged a nd those that are not are discarded.
Thanks
how about this? putting all the array's in an array, so you can loop through them all easily, and use a custom in_array() function to check if they are already existing?
The good thing about this way is that it doesn't limit the script to just four array's, as it will loop through all the array's you get together.
$a = [1,2,3,4,5];
$b = null;
$c = [5,4,3,2,1];
$d = [1,2];
$array_stack = array($a, $b, $c, $d);
$new_array = array();
foreach($array_stack as $stack){
if(!is_array($stack)) continue;
foreach($stack as $value){
if(!in_array($value, $new_array)) $new_array[] = $value;
}
}
print_r($new_array);
maybe something like this :
<?php
$a = [1,2,3,4,5];
$b = null;
$c = [5,4,3,2,1];
$d = [1,2];
$new_array;
if(is_array($a)) $new_array = $a;
if(is_array($b)) $new_array = array_unique(array_merge($new_array,$b));
if(is_array($c)) $new_array = array_unique(array_merge($new_array,$c));
if(is_array($d)) $new_array = array_unique(array_merge($new_array,$d));
?>
Old question, but going to give my input anyways. A more universal approach:
function multiple_array_merge() {
$args = func_get_args();
$array = [];
foreach ($args as $i) {
if (is_array($i)) $array = array_merge($array, $i);
}
return array_unique($array);
}
$a = [1, 2, 3, 4, 5];
$b = null;
$c = [5, 4, 3, 2, 1];
$d = [1, 2];
$merged = multiple_array_merge($a, $b, $c, $d);