I have been trying to find the answer to this one with no luck, so if anyone can help I would really appreciate it. is there a function in PHP which can compare 2 arrays and place matching values in a 3rd array? Also I wonder how I could determine if there were any matches or not, like a boolean.
$array1 = array (1,2,3,4);
$array2 = array (1, 2, 7,8);
//I want to have an array like $array3 after comparing $array1
//and $array2.....also I want to know if values were placed in
//$array3 or not.
$array3 = array(1,2);
You could use array_intersect
From the manual:
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
Which produces:
Array
(
[a] => green
[0] => red
)
If you want to check if there were matches you can do:
empty($result) //true if empty, meaning no matches
Manual entry is here. I borrowed the first example.
$array1 = array (44,2,3,4);
$array2 = array (44,2,7,8);
//I want to have an array like array 3 after comparing $array1
//and $array2.....also I want to know if values were placed in
//$array3 or not.
$array3 = array(44,2);
$result = array_intersect($array1, $array2);
if ($result){
$match = true;
echo $result [0];
}
else{
$match = false;
}
if ($match === true){
// Do something
}
else{
//do something else
}
Related
I have two arrays.
I need to make a foreach where it loops the values that is the same in both arrays. The arrays are not in same order and one of the arrays have more values than the other array.
I could do this.
foreach($array1 as $items1)
{
foreach($array2 as $items2)
{
if($items1 == $items2)
Echo "Match!";
}
}
But that takes a lot of time to load
Edit
I dont get any matches.
Array 1
$array1 = array();
while($fetch = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
{
$array1[] = array("a" => $fetch['value']);
}
Array 2
$filename = "test.txt";
$fp = #fopen($filename, 'r');
if ($fp) {
$array2 = explode("\n", fread($fp, filesize($filename)));
}
CODE
array_unshift($array2,"b");
$result = array_intersect($array1, $array2);
print_r($result);
You can use array_intersect:
$matches = array_intersect($array1, $array2);
You can use PHP in built function array_intersect()
<?php
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
?>
Output
Array
(
[a] => green
[0] => red
)
Note : Returns an array containing all of the values in array1 whose values exist in all of the parameters.
Is there any function similar to "array_intersect" but it is in mode case-insensitive and ignoring tildes?
The array_intersect PHP function compares array elements with === so I do not get the expected result.
For example, I want this code :
$array1 = array("a" => "gréen", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
Outputs gréen and red. In default array_intersect function just red is proposed (normal cause ===).
Any solution ?
Thank you in advance
$result = array_intersect(array_map('strtolower', $array1), array_map('strtolower', $array2));
<?php
function to_lower_and_without_tildes($str,$encoding="UTF-8") {
$str = preg_replace('/&([^;])[^;]*;/',"$1",htmlentities(mb_strtolower($str,$encoding),null,$encoding));
return $str;
}
function compare_function($a,$b) {
return strcmp(to_lower_and_without_tildes($a), to_lower_and_without_tildes($b));
}
$array1 = array("a" => "gréen", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_uintersect($array1, $array2,"compare_function");
print_r($result);
output:
Array
(
[a] => gréen
[0] => red
)
Assume that I have two arrays as follow:
$array1 = array(1, 3, 5);
$array2 = array('x'=> 1, 'y'=> 2, 'z'=> 5);
How to check that the two arrays are exactly the same in most efficient and proper way and it doesn't care the keynames of the *$array2.
I want to make a function which should return true if values are exactly the same, and false if any of the ones are different both in value(s) and number of elements.
Thanks for your time and reading.
In the simplest case you can just use array_diff. It ignores the keys in your second array, but also the order of the values. It would return an empty set if the arrays are equal:
if (count(array_diff($array1, $array2)) == 0) {
// equal
You could also compare the arrays directly, after stripping keys from the second:
if ($array1 == array_values($array2)) {
That would additionally compare the order of contained values.
array_values($array1) === array_values($array2)
Assuming that arrays have same order.
Try this
$array1 = array(1, 3, 5);
$array2 = array('x'=> 1, 'y'=> 2, 'z'=> 5);
$array2 = array_values($array2);
echo $array1 == $array2 ? 'true' : 'false';
array_diff will do the job for you:
<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
if(empty($result)){
// arrays contain the same values!
}
?>
Create a class containing an array and make that class implement the Comparable interface, for example http://php.net/manual/language.oop5.interfaces.php#69467
like this:
<?php
$array1 = array ("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array ("a" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
if(count($result) == 0)
{
.......
}
?>
I have two arrays, array A contains a long list with some elements I want to remove. Array B is the full list of those elements that I wish to remove from array A.
What is the most efficient way to achieve this?
array_diff is the obvious answer, but since you've asked for the most efficient way, here's a test
$big = range(1, 90000);
$remove = range(500, 600);
$ts = microtime(true);
$result = array_diff($big, $remove);
printf("%.2f\n", microtime(true) - $ts);
$ts = microtime(true);
$map = array_flip($remove);
$result = array();
foreach($big as $e)
if(!isset($map[$e]))
$result[] = $e;
printf("%.2f\n", microtime(true) - $ts);
prints on my machine
0.67
0.03
So the simple loop with a hash-based lookup is approximately 20 times faster than array_diff.
Use array_diff()
In the manual it gives for array_dif() this exaple:
<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
?>
Outputs:
Array
(
[1] => blue
)
Returns an array containing all the
entries from array1 that are not
present in any of the other arrays.
You can easily get an array value by its key like so: $value = array[$key] but what if I have the value and I want its key. What's the best way to get it?
You could use array_search() to find the first matching key.
From the manual:
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
You can use the array_keys function for that.
Example:
$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));
This will get the key from the array for value blue
$arr = array('mango', 'orange', 'banana');
$a = array_flip($arr);
$key = $a['orange'];
No really easy way. Loop through the keys until you find array[$key] == $value
If you do this often, create a reverse array/hash that maps values back to keys. Keep in mind multiple keys may map to a single value.
Your array values can be duplicates so it wont give you exact keys. However the way i think is fine is like iterate over and read the keys