Compare 2 arrays in Php - php

I have created a 2 arrays such as below. My goal is to compare these two arrays and echo something IF THERE IS a value in array2 that is NOT in array1.
$array1 = array(1, 2, 3, 4, 5);
$array2 = array(1, 2, 3, 4, 6);

As others have pointed out, you should use array_diff(); array_diff($a, $b) returns the values of $a that are not present in $b.
So:
if (($diff = array_diff($array2, $array1))) {
printf(
"Values in array2 that are not present in array1: %s\n",
join(' ', $diff)
);
}

Use array_diff.
array_diff($array2, $array1);

Use array_diff
<?php
$array1 = array(1, 2, 3, 4, 5);
$array2 = array(1, 2, 3, 4, 6);
$result = array_diff($array1, $array2);
print_r($result);
?>
Output
Array
(
[4] => 6
)
if your want get the same values in array use array_intersect
$result = array_intersect($array1, $array2);

Make use of array_diff().
<?php
$array1 = array(1, 2, 3, 4, 5);
$array2 = array(1, 2, 3, 4, 6);
print_r(array_diff($array2,$array1));
OUTPUT :
Array
(
[4] => 6
)
EDIT :
This is actually the opposite of what I am looking for. What I want is
to scan the array1 to check whether the value of '6' does exist in
array. If it doesn't echo something
<?php
$array1 = array(1, 2, 3, 4, 5);
$array2 = array(1, 2, 3, 4, 6);
foreach($array2 as $val)
{
if(in_array($val,$array1))
{
echo "$val is available in array1<br>";
}
else
{
echo "$val is not available in array1<br>";
}
}
OUTPUT :
1 is available in array1
2 is available in array1
3 is available in array1
4 is available in array1
6 is not available in array1

Related

How to get max element at each index while comparing two arrays?

I have two indexed arrays of identical length:
$first_array = [1,3,4,5,6];
$second_array = [5,2,1,7,9];
I need to generate a new array that consists of the higher value between the two elements at each index.
The output should be:
$output_array[5, 3, 4, 7, 9];
Super easy one-liner:
Pass both arrays to array_map(), as it synchronously loops through both sets of data, call max() on the two elements.
Code: (Demo)
$first_array = [1, 3, 4, 5, 6];
$second_array = [5, 2, 1, 7, 9];
var_export(array_map('max', $first_array, $second_array));
Output:
array (
0 => 5,
1 => 3,
2 => 4,
3 => 7,
4 => 9,
)
Try this way. demo
<?php
$first_array = array(1,3,4,5,6);
$second_array = array(5,2,1,7,9);
$return = array();
foreach($first_array as $key => $value){
if($first_array[$key] > $second_array[$key]){
$return[] = $first_array[$key];
}else{
$return[] = $second_array[$key];
}
}
print_r($return);

Add value from one element to other in php

This is probably a duplicate, but I couldn't find the answer I needed, maybe my wording is wrong.
Anyway, I have a two-dimensional array with hundreds of values, what I need is to insert a value from second element to the first element.
Example;
I have four elements in an array:
[0] = 1, [1] = 9, [2] = 9, [3] =5
I need to put these into a single element, so that it would turn
into this: [0] = 1995.
I have a feeling there might be something I could do with foreach, if so, maybe someone can explain to me, in detail, how that would exactly work?
OR
Maybe there's a function I'm not aware of.
You can use the following, using implode:
$arr = [1, 9, 9, 5];
$val = implode($arr);
unset($arr);
$arr[0] = $val;
demo: https://ideone.com/VTb7vO
To use the solution using implode on the whole multidimensional array, you can use the following:
$arr = [[1, 9, 9, 5], [1, 9, 9, 6], [1, 9, 9, 7], [1, 9, 9, 8]];
foreach ($arr as $key => $value) {
$val = implode($value);
$arr[$key] = $val;
}
demo: https://ideone.com/X6vueO
another, much shorter solution could be the following:
$arr = [[1, 9, 9, 5], [1, 9, 9, 6], [1, 9, 9, 7], [1, 9, 9, 8]];
$arr = array_map('implode', $arr);
demo: https://ideone.com/0ju8he
To concatenate each inner array of numbers you can use implode on each of it.
$newArray = array_map('implode', $array);
If it is executed on the array [[1, 2, 3], [1, 3], [1, 1, 1]] it will create the array ['123', '13', '111']. demo
$arr = [2,3,4];
$var = implode($arr, '');
var_dump( $var );
Two dimensional:
$cars = array
(
array(22,18),
array(15,13),
array(5,2),
array(17,15)
);
foreach ($cars as $val) {
$var1 = implode($val, '');
var_dump( $var1 );
}
$var= "";
foreach($arr as $element) {
$var .= $element;
}

PHP - combine multiple arrays (only occurrences of all arrays)

Is there any php function that can return occurrences of all provided arrays (that are in all arrays)?
Example:
$array1 = array(1, 2, 3);
$array2 = array(1, 2, 4, 5, 6);
$array3 = array(1, 2, 3, 4, 5);
func_i_am_looking_for($array1, $array2, $array3);
//retrun array(1, 2);
Just try with array_intersect
$result = array_intersect($array1, $array2, $array3);

Rewriting an Array keys after sorting it

$arr1 = [1, 2, 3, 8];
$arr2 = [1, 2, 4, 9, 10];
$arr3 = [1, 2, 5, 11, 12];
$arrs = [$arr1, $arr2, $arr3];
arsort($arrs);
I have sorted the $arrs to change it to $arr3, $arr2, $arr1, My problem is that it kept its Array Key as it is, I want to rewrite these keys by its new order, so instead of
[2]$arr3 [1]$arr2 [0]$arr1
it becomes
[0]$arr3 [1]$arr2 [2]$arr1
I thought about explode()ing then implode()ing the array again But it didn't work because it is a MDArray like the following $arrs = implode(explode($arrs)); after arsort().
What is the best and shortest way to re[write][make] the array keys?
Just simply use array_values;
$arr1 = [1, 2, 3, 8];
$arr2 = [1, 2, 4, 9, 10];
$arr3 = [1, 2, 5, 11, 12];
$arrs = [$arr1, $arr2, $arr3];
arsort($arrs);
$arrs = array_values($arrs);
This will reset the keys based on the order.
You only need rsort if you don't need to keep key
$arr1 = [1, 2, 3, 8];
$arr2 = [1, 2, 4, 9, 10];
$arr3 = [1, 2, 5, 11, 12];
$arrs = [$arr1, $arr2, $arr3];
rsort($arrs);
print_r($arrs);
DEMO
I think you need to use rsort() instead of arsort() because, the latter will preserve the indices when sorting, while the first will reorder the values regardless of the keys.

PHP compare arrays

I have two arrays. $arrayOld and $arrayNew and I want to compare these arrays and only select the values that are not in $arrayNew.
I don't want the values that are in $arrayNew only. So I don't think array_diff() is gonna help me.
$arrayOld = [1, 2, 3, 4, 5]
$arrayNew = [1, 4, 5, 6, 7]
So it only needs to return 2 and 3 and not 6 or 7.
use array_diff, to accomplish this. As you need to difference between the array and need data from Old array so you need to use the old array as the first parameter of the array_diff.
Note: Array diff only returns from the first array which is not in second array.
$arrayOld = [1, 2, 3, 4, 5];
$arrayNew = [1, 4, 5, 6, 7];
$n = array_diff($arrayOld, $arrayNew);
print_r($n);
Result: Online Check
Array
(
[1] => 2
[2] => 3
)
If you need a new keys for the output array just use array_values. The new key start from 0.
$arr = array_values($n);
Use below code
$arrayOld = [1, 2, 3, 4, 5];
$arrayNew = [1, 4, 5, 6, 7];
print "<pre>";
print_r(array_diff($arrayOld, $arrayNew));
OUTPUT:
Array
(
[1] => 2
[2] => 3
)
use this code.
$arrayOld = array(1, 2, 3, 4, 5);
$arrayNew = array(1, 4, 5, 6, 7);
print_r(array_diff($arrayOld, $arrayNew));
$compare = array();
$i=1;
foreach($arrayOld as $k=>$v){
if(!in_array($v, $arrayNew)){
$compare[$i] = $v;
$i++;
}
}
$i=$i;
foreach($arrayNew as $k=>$v){
if(!in_array($v, $arrayOld)){
$compare[$i] = $v;
$i++;
}
}
use array_diff function
$arrayOld = array(1, 2, 3, 4, 5);
$arrayNew = array(1, 4, 5, 6, 7);
print_r(array_diff($arrayOld, $arrayNew));

Categories