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

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

Related

Parse error: syntax error, unexpected '...' (T_ELLIPSIS) while merging two arrays

What is the problem here?
$arr1 = [1, 2, 3];
$arr2 = [4, 5, 6];
$arr4 = [...$arr1, ...$arr2];
print_r($arr4);
you can do like this
$arr1 = [1, 2, 3];
$arr2 = [4, 5, 6];
//$arr4 = [...$arr1, ...$arr2]; **[ Replace this line]**
$arr4 = array_merge($arr1, $arr2);
print_r($arr4);

Reorder elements in array using php

I have an array of elements like this:
$data = array(1, 2, 3, 4);
How can I reorder for example, starting from second element to get 2, 3, 4, 1; or starting from third element to get 3, 4, 1, 2?
One solution is to use array_slice function to separate the two portions and combine them with array_merge:
$data = [1, 2, 3, 4];
$pos = 2;
$ordered = array_merge(
array_slice($data, $pos),
array_slice($data, 0, $pos)
);
// [3, 4, 1, 2]
You can use array_splice
$data = array(1,2,3,4);
$out = array_splice($data, 1, 3);
array_splice($data, 0, 0, $out);
print_r($data);
You could just array_shift() and then push the shifted value back on to the end of the Array:
$data = array(1,2,3,4);
$data[] = array_shift($data);

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

Compare 2 arrays in 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

Categories