I want to compare two arrays in PHP - php

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)
{
.......
}
?>

Related

php update array values

I have two arrays with different length, but keys are similar.
My requirement is update $array1 with values of $array2 similar keys
$array1 = array("Jan"=>"0", "Feb"=>"0", "Mar"=>"0", "Apr"=>"0");
$array2 = array("Jan"=>"2", "Mar"=>"3");
Output:
$res = array("Jan"=>"2","Feb"=>"0","Mar"=>"3","Apr"=>"0");
You can achieve it by this code:
$array1 = array("Jan" => "0", "Feb" => "0", "Mar" => "0", "Apr" => "0");
$array2 = array("Jan" => "2", "Mar" => "3");
$array3 = array_replace($array1, $array2);
print_r($array3);
You can simply use + operator.
$array1 = array("Jan"=>"0","Feb"=>"0","Mar"=>"0","Apr"=>"0");
$array2 = array("Jan"=>"2", "Mar"=>"3");
print_r($array2 + $array1);
DEMO
Try this:
array_merge($array1, $array2);

Find shared vales in array, put into anothyer array

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
}

PHP array_intersect case-insensitive and ignoring tildes

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
)

PHP, how to get the Array Key in Single Dimensional Array.

In PHP I create an array of key value pairs like this, how to I get "mykey"?
$arr = array("mykey"=>"myvalue");
I know the code below will work, but I am interested to know if there is a language construct in PHP that allows me to this in a easier fashion.
foreach($arr as $key=>$value){
$result = $key;
break;
}
echo $result;
Just Use key if you dealing with one element and array_keys with arrays with multiple values.
Example
$arr = array("mykey"=>"myvalue");
var_dump(key($arr));
Or
var_dump(array_keys($arr));
Output
string 'mykey' (length=5)
and
array
0 => string 'mykey' (length=5)
try this
you can use array_keys function in php
<?php
$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));
$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));
$array = array("color" => array("blue", "red", "green"),
"size" => array("small", "medium", "large"));
print_r(array_keys($array));
?>
you need to use array_keys, here's the manual

php - how to check if at least one element of first array exists in second array

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;

Categories