PHP merging arrays uniquely - php

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);

Related

How to replace array value with another array value using php?

I have 2 arrays as follows:
$arr = array('one.jpg', 'two.jpg', 'three.jpg');
$arr1 = array('', 'five.jpg', '');
Now I want to make these two arrays to one array with following value:
$newArray = array('one.jpg', 'five.jpg', 'three.jpg');
How can I do this using PHP?
Use array_filter to remove the empty values.
Use array_replace to replace the values from the first array with the remaining values of the 2nd array.
$arr1=array_filter($arr1);
var_dump(array_replace($arr,$arr1));
Assuming you want to overwrite entries in the first array only with truthy values from the second:
$newArray = array_map(function ($a, $b) { return $b ?: $a; }, $arr, $arr1);
You can iterate through array and check value for second array :
$arr = array('one.jpg', 'two.jpg', 'three.jpg');
$arr1 = array('', 'five.jpg', '');
$newArray =array();
foreach ($arr as $key => $value) {
if(isset($arr1[$key]) && $arr1[$key] != "")
$newArray[$key] = $arr1[$key];
else
$newArray[$key] = $value;
}
var_dump($newArray);
Simple solution using a for loop, not sure whether there is a more elegant one:
$arr = array('one.jpg', 'two.jpg', 'three.jpg');
$arr1 = array('', 'five.jpg', '');
$newArray = array();
$size = count($arr);
for($i = 0; $i < $size; ++$i) {
if(!empty($arr1[$i])){
$newArray[$i] = $arr1[$i];
} else {
$newArray[$i] = $arr[$i];
}
}

Explode an array php

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);

sum the individual columns of 3 different arrays

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);

How do add new index to array in foreach loop?

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.

Select values from an array by array of keys

I have two arrays of the same length containing some values.
$a = array("a","b","x","x");
$b = array("f","g","g","h");
Now I want to get the values from $b at the index postions from where $a is x.
$ids = array_keys($a, 'x');
$res = ???($b,$ids);
print_r($res);
So what function will give me an Array containing g and h. Or is there even a more elegent (e.g. not using array_keys()) to do this?
$needle = 'x';
$res = array();
foreach($a as $key => $value) {
if ($value == $needle) {
$res[] = $b[$key];
}
}

Categories