I have two arrays and I want to compare the first with the second array. I have an IF statement and it currently does two checks:
It checks to see if there are any rows from my query
If the above passes, then I want to traverse the array values of the first array and compare it with the second array. I want to traverse through all the names first array (index 0-9) and compare it with the first name of the second array (index 0). Keep doing the same thing until we've compared all the indices 0-9 on the first array with all indices 0-9 of the second array.
Finally, if there is a match I want to exit my PHP script, else if there is no match I continue to do to stuff.
I have tried using the in_array() function and that works, but only with a single variable. Something like this in_array($firstArray[0], $secondArray) works. Something like this:
if(mysqli_num_rows($result) > 0 && in_array($firstArray[0], $secondArray)){
exit("exiting now!");
}
else {
echo "continue to do stuff...";
}
However, when I put the whole array in the inArray, it won't work. Like this:
in_array($firstArray, $secondArray)
How can I achieve this?
You can use array_intersect if you want to know if they intersect, you can use array_diff to see if the two arrays have any differences.
if(mysqli_num_rows($result) > 0 && !array_diff($firstArray, $secondArray)){
exit("exiting now!");
} else {
echo "continue to do stuff...";
}
Example:
<?php
$array1 = array (1,2,3,4);
$array2 = array (3,2,1,5);
$array3 = array_diff($array1, $array2);
$array4 = array_intersect($array1, $array2);
echo "<pre>";
echo "Array 1\n ";
print_r($array1);
echo "Array 2\n ";
print_r($array2);
echo "Arrays Difference \n ";
print_r($array3);
echo "Arrays intersect \n ";
print_r($array4);
echo "</pre>";
?>
Output:
Array 1
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Array 2
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 5
)
Arrays Difference
Array
(
[3] => 4
)
Arrays intersect
Array
(
[0] => 1
[1] => 2
[2] => 3
)
I think you are looking for array_intersect() which returns the values that are common in two (or more) arrays. http://php.net/manual/en/function.array-intersect.php
It returns an array but if it returns an empty array then empty arrays are falsy.
Related
I have the following array structure with master data with thousands of records.
Array
(
[0] => 0,1
[1] => 0,0
[2] => 0,2
[3] => 0,3
[4] => 10,2
)
I have a second array with a smaller subset. Such as
Array
(
[0] => 0,1
[1] => 0,0
)
I would like to find second array in first array in the extact same order of elements present in second array. But rather than doing an intersect I would like to find the key (or keys) from first array as well. I have been wrecking my brain on this...
UPDATED:
The keys are unique . So far example in above array I would like to see output of:
Array2 found in array1 (starting at key 0).
Second Example:
Array
(
[a] => 0,1
[b] => 0,0
[c] => 0,2
[d] => 0,3
[e] => 10,2
)
second array
Array
(
[1] => 0,3
[2] => 10,2
)
expected output:
second array match in array A, starting at key d of array A..
hope that clears it.
The best solution to get matching keys is
$result_array = array_intersect_assoc($array1, $array2);
$result_array_keys = array_keys(array_intersect_assoc($array1, $array2));
print_r($result_array);
print_r($result_array_keys); // this gives matching keys array
I don't understand what is wrong with array_intersect.
As you describe your expected output it seems as array_intersect is perfect.
$a = Array
(
15 => "0,1",
16 => "0,0",
2 => "0,2",
3 => "0,3",
4 => "10,2"
);
$b = Array
(
0 => "0,1",
1 => "0,0"
);
Var_dump(array_intersect($a, $b));
Output:
array(2) {
[15]=> string(3) "0,1"
[16]=> string(3) "0,0"
}
https://3v4l.org/KG1v6
Or if MonkeyZeus is correct maybe this can work for you?
I match array_intersect, then make sure the keys is the same.
$intersect = array_intersect($a, $b);
$keys = array_keys($intersect);
If($keys == array_keys($b)){
Echo "they match";
}else{
Echo "don't match";
}
https://3v4l.org/iiNmd
After OPs edit it seems it is a simple array_intersect that is needed.
https://3v4l.org/gIb10
$intersect = array_intersect($a, $b);
Var_dump($intersect);
Echo "matching keys is: " . Implode(", ", array_keys($intersect));
None of the solutions posted above work. What helped me is to re-structure my array and answer from here: Find array in array, in sequence
Is it possible in PHP to cross check all matches of arrays and store the result to an array? I found `array_intersect(), but this function only gives results of the first array back. And only if the value are in all arrays present.
Example input:
Array 1 = 1,2,3
Array 2 = 4,5,6
Array 3 = 6,7,8
Array 4 = 3,9,10
Now I need an array which gives me any match back. In this example it would be:
result = 3,6
Because "3" is in array 1 and array 4 and result "6" is in array 2 and array 3.
Any match which is in more than one array. Any hints/ideas?
This should work for you:
First you get all unique values from each array with array_unique(). Then you merge them together into one array with array_merge() and count all values with array_count_values().
After this you simply filter all values out with array_filter(), which aren't in more than 1 array and you have your expected result.
To flip the array back, just use array_keys().
<?php
$count = array_count_values(array_merge(array_unique($arr1), array_unique($arr2), array_unique($arr3), array_unique($arr4)));
$result = array_keys(array_filter($count, function($v){
return $v >= 2;
}));
print_r($result);
?>
output:
Array
(
[0] => 3
[1] => 6
)
You are asking for an intersection: http://php.net/manual/en/function.array-intersect.php
In your case you will need logic to perform the intersection on every combination of two arrays.
I am trying to compare two different arrays and get the values that do not exist in 1 of the arrays. Here are my 2 arrays:
Array ( [0] => 2fbd5868-28ec-418d-854a-0736db720c8a [1] => f4a41974-5373-4862-a5e7-9d28b8c2301f [2] => a1874f68-3da1-47c3-97ef-a68580ce2a52)
Array ( [0] => 2fbd5868-28ec-418d-854a-0736db720c8a [1] => f4a41974-5373-4862-a5e7-9d28b8c2301f [2] => a1874f68-3da1-47c3-97ef-a68580ce2a52 [3] => 583cee91-1913-4e9d-b51d-e27083420001)
As you can see the second array has an additional value. I am trying to user array_diff like this:
$result = array_diff($array1,$array2);
print_r($result);
However the out of the array_diff is:
array()
Any ideas what is going on?
As people have suggested and i have already tested switching the arrays around, this is the output:
Array ( [0] => [1] => )
array_diff gives you the values from $array1 that are not in the other arrays. All the values of your first array are in the second. Sou change the order of your arrays and you should be fine.
See also here: http://php.net/manual/de/function.array-diff.php
The order of arguments in array_diff() is important
Returns an array containing all the entries from array1 that are not
present in any of the other arrays2
Read array_diff
$result = array_diff($array2,$array1);
Try like this
I have two arrays (in PHP):
ArrayA
(
[0] => 9
[1] => 1
[2] => 2
[3] => 7
)
ArrayB
(
[0] => 1
[1] => 1
[3] => 8
)
I want to make two new arrays, where I have only the elements declared in both of the arrays, like the following:
ArrayA
(
[0] => 9
[1] => 1
[3] => 7
)
ArrayB
(
[0] => 1
[1] => 1
[3] => 8
)
In this example ArrayA[2] doesn't exist, so ArrayB[2] has been unset.
I wrote this for loop:
for ($i = 0, $i = 99999, $i++){
if (isset($ArrayA[$i]) AND isset($ArrayB[$i]) == FALSE)
{
unset($ArrayA[$i],$ArrayB[$i]);
}
}
But it's not great because it tries every index between 0 and a very big number (99999 in this case). How can I improve my code?
The function you're looking for is array_intersect_key:
array_intersect_key() returns an array containing all the entries of array1 which have keys that are present in all the arguments.
Since you want both arrays, you'll have to run it twice, with the parameters in opposite orders, as it only keeps keys from the first array. An example:
$arrayA_filtered = array_intersect_key($arrayA, $arrayB);
$arrayB_filtered = array_intersect_key($arrayB, $arrayA);
Also, although a for loop wasn't ideal in this case, in other cases where you find yourself needing to loop through sparse array (one where not every number is set), you can use a foreach loop:
foreach($array as $key => $value) {
//Do stuff
}
One very important thing to note about PHP arrays is that they are associative. You can't simply use a for loop, as the indices are not necessarily a range of integers. Consider what would happen if you applied this algorithm twice! You'd get out of bounds errors as $arrayA[2] and $arrayB[2] no longer exist!
I would iterate through the arrays using nested foreach statements. I.e.
$outputA = array();
$outputB = array();
foreach ($arrayA as $keyA => $itemA) {
foreach ($arrayB as $keyB => $itemB) {
if ($keyA == $keyB) {
$outputA[$keyA] = $itemA;
$outputB[$keyB] = $itemB;
}
}
This should give you two arrays, $outputA and $outputB, which look just like $arrayA and $arrayB, except they only include key=>value pairs if the key was present in both original arrays.
foreach($arrayA as $k=>$a)
if (!isset($arrayB[$k]))
unset($arrayA[$k];
Take a look to php : array_diff
http://docs.php.net/manual/fr/function.array-diff.php
I would like to compare two array items with php, I think I should use array_intersect_key but I don't know how I can do that.
Array 1
[1] => obj Object
(
[idobj:protected] => 2
)
[2] => obj Object
(
[idobj:protected] => 1
)
Array 2
[1] => obj Object
(
[idobj:protected] => 1
)
No, you don't need to use array_intersect_key() if you need only to compare array elements.
It simple like this (for two-dimensional arrays):
if( $array1[0] == $array2[0] ) {
echo 'Array items are equal';
} else {
echo 'Array items are not equal';
}
If you have multi-dimensional array you may need add some extra indexes.
PHP manual has a very good information regarding arrays, check it out.
Are you actually looking for array_intersect()?
$objectsInArray1ThatArePresentInArray2 = array_intersect($array1, $array2);