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];
}
}
Related
This question already has answers here:
How to merge two arrays by summing the merged values [duplicate]
(3 answers)
Closed 5 months ago.
I've searched around 1 h. It seems to be duplicate, but I can't find something to help me.
$a = array('aa'=>10, 'bb'=>2, 'cc'=>5);
$b = array('aa'=>1, 'bb'=>20, 'dd'=>11);
$c = array('aa'=>3);
$d = array('ee'=>5);
How can I sum all of this in one array which should look like:
$summed = array('aa'=>14,'bb'=>22,'cc'=>5,'dd'=>11,'ee'=>5);
Merge all arrays and sum values if it's an array
$res = array_merge_recursive($a, $b, $c,$d);
foreach($res as &$x) {
if (is_array($x)) {
$x = array_sum($x);
}
}
print_r($res);
demo
You could try something like this :
$summed=[] ;
foreach ([$a,$b,$c,$d] as $arr) {
foreach ($arr as $k => $v) {
if (!isset($summed[$k])) $summed[$k] = 0;
$summed[$k]+=$v;
}
}
print_r($summed);
Will outputs :
Array
(
[aa] => 14
[bb] => 22
[cc] => 5
[dd] => 11
[ee] => 5
)
$a = array('aa'=>10, 'bb'=>2, 'cc'=>5);
$b = array('aa'=>1, 'bb'=>20, 'dd'=>11);
$c = array('aa'=>3);
$d = array('ee'=>5);
$arrays = [$a, $b, $c, $d];
$result = [];
foreach ($arrays as $value_array) {
foreach ($value_array as $key => $value) {
if (array_key_exists($key, $result)) {
$result[$key] += $value;
} else {
$result[$key] = $value;
}
}
}
A method that uses less looping than iterating the full arrays is to get all keys from the arrays and use array_column and array_sum.
$a = array('aa'=>10, 'bb'=>2, 'cc'=>5);
$b = array('aa'=>1, 'bb'=>20, 'dd'=>11);
$c = array('aa'=>3);
$d = array('ee'=>5);
$arr = [$a,$b,$c,$d];
$keys = array_keys(array_merge($a,$b,$c,$d)); //merge arrays and get keys ['aa','bb'] etc.
Foreach($keys as $key){
// Sum column of 'aa', 'bb' etc from array
$sums[$key] = array_sum(array_column($arr, $key));
}
Var_dump($sums);
https://3v4l.org/ZHtWf
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];
}
}
I have an array like the one below:
$v = array(1,2,3,4,2,3);
How do I get the keys of all elements in array where the value is equal to 2?
If you have a value in an array and you want to get the keys you can use array_keys() with the optional search_value:
$v = array(1,2,3,4,2,3);
$keys = array_keys($v, '2');
print_r($keys);
// Array
// (
// [0] => 1
// [1] => 4
// )
For the output check https://3v4l.org/N8EBH
You can do it like
<?php
$v = array(1,2,3,4,2,3);
$keys = array();
foreach($v as $k=>$x)
{
if($x == 2)
$keys[] = $k;
}
echo "<pre>";print_r($keys);echo "</pre>";
Try this code
$v = array(1,2,3,4,2,3);
$a = 2;
$key = array();
foreach($v as $k=>$val)
{
if($a == $val)
{
$key[] = $k;
}
}
print_r($key);
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);
If i have two array as following:
$array1 = array(array('id'=>11,'name'=>'Name1'),array('id'=>22,'name'=>'Name2'), array('id'=>33,'name'=>'Name3'),array('id'=>44,'name'=>'Name4'),array('id'=>55,'name'=>'Name5'));
$array2 = array(array('id'=>22,'name'=>'Name2'),array('id'=>55,'name'=>'Name5'));
My expect result, the array2 should be always at the beginning :
$newarray = array(array('id'=>22,'name'=>'Name2'),array('id'=>55,'name'=>'Name5'), array('id'=>11,'name'=>'Name1'), array('id'=>33,'name'=>'Name3'),array('id'=>44,'name'=>'Name4'));
My current solution is using two for loops:
foreach($array2 as $Key2 => $Value2) {
foreach($array1 as $Key1 => $Value1){
if($Value1['id'] != $Value2['id']) {
//push array
}
}
}
Edit:
The result "$newarray" should not include the duplicate ids from array1.
But i am looking for a faster and simpler solution.
SOLUTION:
$a1 = array();
foreach ($array1 as $v) $a1[$v['uuid']] = $v;
$a2 = array();
foreach ($array2 as $v) $a2[$v['uuid']] = $v;
$filtered = array_values(array_diff_key($a1, $a2));
//print_r($filtered);
$newarray = array_merge($array2, $filtered);
Thank you guys!!!!
Thanks.
Regards Jack
you want $new_array = array_merge($array2, $array1); puts the second array onto the end of the first one
You can use array_merge() function for that, like this:
$newarray = array_merge($array2, $array1);
I am not sure about your requirement but to sort multi-dimensional array on some specific key
You need to use usort function
Try the code below:
$cmp = function ($a, $b){
$a_val = $a['id'];
$b_val = $b['id'];
if ( $a_val == $b_val) {
return 0;
}
return ($a_val < $b_val) ? -1 : 1;
};
usort($array2,$cmp);
$array2 will be sorted by 'id'