Minimum elements of two arrays - php

guys!
Does the PHP have a ready-made function for finding the minimum elements from two different arrays?
For example:
$arr1 = [1,4,5,8];
$arr2 = [2,3,6,7];
$arr3 = [];
I have to compare $arr1[0] and $arr2[0] and push minimum of them into $arr3
$arr3 should contain [1,3,5,7]

If you're fine with two function, you can use:
$arr3 = array_map('min',$arr1,$arr2);

Related

How to merge two array without items that exists in both arrays in PHP?

I'm having 2 arrays:
$arr_1 = ['1','2','3','4'];
$arr_2 = ['2','5','4','6','7'];
I want to merge $arr_1 and $arr_2 with out exists item in both of these arrays like:
$arr_merged = ['1','3','5','6','7']
How could I do that? I think I have to run some foreach to loop through these arrays and handle, but it's looks like not a really good resolution for this problem...
For getting unique items, the array_diff() function can be used to find the difference between two arrays. It computes the difference of all the arrays and returns all the arrays that do contains entries that cannot be matched in any of the arrays. And for merging two arrays you can use array_merge() function.
<?php
$arr_1 = ['1','2','3','4'];
$arr_2 = ['2','5','4','6','7'];
$arr_merged = array_merge(array_diff($arr_1, $arr_2), array_diff($arr_2, $arr_1));
print_r($arr_merged);
?>
You use array_diff along with array_merge to achieve the output.
Note: array_diff is technically not the opposite of array_intersect.
<?php
$arr_1 = ['1','2','3','4'];
$arr_2 = ['2','5','4','6','7'];
$diff_1 = array_diff($arr_1, $arr_2);
$diff_2 = array_diff($arr_2, $arr_1);
$array_merge = array_merge($diff_1,$diff_2);
print_r($array_merge);
?>

Php single Array Comparison

I have two arrays.
For example
$arr1 = ['apple','mango','grapes','banana'];
$arr2 = ['apple','mango','orange'];
Now I want to compare these two arrays and make it as
$arr3 = ['apple','mango','grapes','banana']
That is, compare $arr2 with $arr1. If the value of $arr2 exists(apple and mango) in $arr1, then add it in $arr3 otherwise remove that element from $arr2 (orange).
Note: $arr1 values remains the same. i.e - should not remove grapes and banana
I have tried array_intersect and array_unique. But struck with the logic.
#vinothini simply do it with array_intersect() and array_diff() like below:
<?php
$arr1 = ['apple','mango','grapes','banana'];
$arr2 = ['apple','mango','orange'];
$arr3 = array_merge(array_intersect($arr2, $arr1), array_diff($arr1, $arr2));
print_r($arr3);
But the main thing about your answer is that in your case $arr3 will alway equals to $arr1 you don't have to calculate anything so right answer is:
$arr3 = $arr1
you can directly take $arr1 as your desired array()
This code will do.
$arr3 = array_intersect($arr1, $arr2);
$arr4 = array_diff($arr1,$arr3);
$output = array_merge($arr3,$arr4);

Copy into `arr3` all those entries from `arr1` that exist in `arr2`

What is the easiest way to copy into arr3 all those entries from arr1 that exist in arr2? Is it possible to do this without using FOR loop?
You can do this using array_intersect:
$arr3 = array_intersect($arr1, $arr2);
Or, if $arr3 has items you wish to preserve, you can merge the old array and the result of the function call:
$arr3 = array_merge($arr3, array_intersect($arr1, $arr2));

How to Combine two arrays randomly in PHP

How to combine two arrays into single one and i am requesting this in such a way that the 3rd combination array should contains one value from one array and the next one from other array and so on.. or ( it could be random)
ex:
$arr1 = (1, 2, 3, 4, 5);
$arr2 = (10, 20, 30, 40, 50);
and combined array
$arr3 = (1, 10, 2, 20, 3, 30, ...);
If it can be random, this will solve your problem:
$merged = array_merge($arr1, $arr2);
shuffle($merged);
I also made a function for fun that will produce the exact output you had in your question. It will work regardless of the size of the two arrays.
function FosMerge($arr1, $arr2) {
$res=array();
$arr1=array_reverse($arr1);
$arr2=array_reverse($arr2);
foreach ($arr1 as $a1) {
if (count($arr1)==0) {
break;
}
array_push($res, array_pop($arr1));
if (count($arr2)!=0) {
array_push($res, array_pop($arr2));
}
}
return array_merge($res, $arr2);
}
This will return a random array:
$merged = array_merge($arr1,$arr2);
shuffle($merged);
sort($arr3 = array_merge($arr1, $arr2));
array_merge() will merge your arrays into one. sort() will sort the combined array.
If you want it random instead of sorted:
shuffle($arr3 = array_merge($arr1, $arr2));
$arr3 contains the array you're looking for.
You can use
<?php
arr3 = array_merge ($arr1 , $arr2 );
print_r(arr3);
?>
which will output in
$arr3 = (1,2,3,4,5,10,20,30,40,50)

How can I use in_array if the needle is an array?

I have 2 arrays, the value will be loaded from database, below is an example:
$arr1 = array(1,2,3);
$arr2 = array(1,2,3,4,5,6,7);
What I want to do is to check if all the values in $arr1 exist in $arr2. The above example should be a TRUE while:
$arr3 = array(1,2,4,5,6,7);
comparing $arr1 with $arr3 will return a FALSE.
Normally I use in_array because I only need to check single value into an array. But in this case, in_array cannot be used. I'd like to see if there is a simple way to do the checking with a minimum looping.
UPDATE for clarification.
First array will be a set that contains unique values. Second array can contain duplicated values. They are both guaranteed an array before processing.
Use array_diff():
$arr1 = array(1,2,3);
$arr2 = array(1,2,3,4,5,6,7);
$arr3 = array_diff($arr1, $arr2);
if (count($arr3) == 0) {
// all of $arr1 is in $arr2
}
You can use array_intersect or array_diff:
$arr1 = array(1,2,3);
$arr2 = array(1,2,3,4,5,6,7);
if ( $arr1 == array_intersect($arr1, $arr2) ) {
// All elements of arr1 are in arr2
}
However, if you don't need to use the result of the intersection (which seems to be your case), it is more space and time efficient to use array_diff:
$arr1 = array(1,2,3);
$arr2 = array(1,2,3,4,5,6,7);
$diff = array_diff($arr1, $arr2);
if ( empty($diff) ) {
// All elements of arr1 are in arr2
}
You can try use the array_diff() function to find the difference between the two arrays, this might help you. I think to clarify you mean, all the values in the first array must be in the second array, but not the other way around.
In my particular case I needed to check if a pair of ids was processed before or not. So simple array_diff() did not work for me.
Instead I generated keys from ids sorted alphabetically and used them with in_array:
<?php
$pairs = array();
// ...
$pair = array($currentId, $id);
sort($pair);
$pair = implode('-', $pair);
if (in_array($pair, $pairs)) {
continue;
}
$pairs[$pair] = $pair;
This is probably not an optimum solution at all but I just needed it for a dirty script to be executed once.

Categories