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
)
Related
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
}
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 in PHP which will give an array of uncommon values from two or more arrays?
For example:
$array1 = array( "green", "red", "blue");
$array2 = array( "green", "yellow", "red");
....
$result = Function_Needed($array1, $array2,...);
print_r($result);
Should give the output:
array("blue", "yellow", ...);
Use array_diff and array_merge:
$result = array_merge(array_diff($array1, $array2), array_diff($array2, $array1));
Here's a demo.
For multiple arrays, combine it with a callback and array_reduce:
function unique(&$a, $b) {
return $a ? array_merge(array_diff($a, $b), array_diff($b, $a)) : $b;
}
$arrays = array(
array('green', 'red', 'blue'),
array('green', 'yellow', 'red')
);
$result = array_reduce($arrays, 'unique');
And here's a demo of that.
$result = array_diff($array1, $array2) + array_diff($array2, $array1);
Try array_diff.
This should do it. It can be extended to work with more than two arrays. It basically counts the common key occurrences and returns those with count of one:
$a = array('yellow', 'blue', 'red', 'green');
$b = array('blue', 'purple', 'green');
function unintersect($a, $b)
{
$x = array_fill_keys($a, 1);
foreach ($b as $v) {
$x[$v]++; // this might trigger warning
}
return array_keys(array_filter($x, function($v) {
return $v === 1;
}));
}
print_r(unintersect($a, $b));
Returns:
Array
(
[0] => yellow
[1] => red
[2] => purple
)
I have two arrays : array("blue", "yellow") and array("blue", "green", "red", "purple"). Is there a function that will check if those two arrays have at least one element value in common ("blue") - just return true or false.
$array1 = array("blue", "yellow");
$array2 = array("blue", "green", "red");
return count(array_intersect($array1, $array2)) > 0;
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)
{
.......
}
?>