PHP - Check if two arrays are equal
$arraysAreEqual = ($a == $b); // TRUE if $a and $b have the same key/value pairs.
$arraysAreEqual = ($a === $b); // TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
See Array Operators.
EDIT
The inequality operator is !=
while the non-identity operator is !==
to match the equality
operator ==
and the identity operator ===
.
Compare array items against other items in array one time
You could use two for loops
$count = count($z);
for ($i = 0; $i < $count - 1; $i++) {
for ($j = $i + 1; $j < $count; $j++) {
// compare $z[$i] with $z[$j]...
}
}
The other loop goes from 0 to n - 2 and the inner loop takes the value from the outer value as initial and goes to the end.
If you also need to handle associative arrays, like one commenter mentioned, you could use array_values
(and array_keys
) to handle this case.
$values = array_values($z);
$keys = array_keys($z); // optional - only used, if you need the keys..
$count = count($values);
for ($i = 0; $i < $count - 1; $i++) {
for ($j = $i + 1; $j < $count; $j++) {
// compare $values[$i] with $values[$j]...
// or $keys[$i] with $keys[$j];
}
}
Note: A
foreach
loop won't work in either case, because you can't
told the inner loop to skip all values, already seen in the outher
loop. And teaching theforeach
loop this behavior would result in a
foreach
, that effectly looks like a simplefor
loop and is slower.
There's simply no benefit in using aforeach
.
PHP compare 2 arrays and get only the non matching values
array_diff — Computes the difference of arrays
$array1 = array("verbal aggression", "physical aggression", "vol");
$array2 = array("verbal aggression", "physical aggression");
$result=array_diff($array1,$array2);
print_r($result);
Output :
Array
(
[2] => vol
)
PHP compare two arrays of dates and check for match
You can do it this way too:
$result_array = array();
$found = false;
for($i=0; $i<count($vacation_array); $i++) {
$found = false;
for($j=0; $j<count($vacation_array[$i]['days']); $j++) {
if(in_array($vacation_array[$i]['days'][$j], $array_1) && !$found) {
$result_array[] = $vacation_array[$i];
$found = true;
}
}
}
print_r($result_array);
Compare two array and get all differences
try this
array_merge(array_diff($array1,$array2),array_diff($array2,$array1))
Compare multidimensional array with another array in PHP and get the values from the multidimensional array
For your example data, you could use array_reduce and use in_array to check if the tag
is present in $compare_array
If you must make a variable with the name from the $compare_array
and append the value to the variable you might use extract with a flag that suits your expectations.
The value
is not present in all the example data so you might also check if that exists.
$compare_array = array('DOCUMENT', 'SENDERID', 'SENDERSHORTNAME', 'RECIPIENTID');
$result = array_reduce($arrays, function($carry, $item) use ($compare_array) {
if(isset($item["value"]) && in_array($item["tag"], $compare_array, true)) {
$carry[$item["tag"]] = $item["value"];
}
return $carry;
});
extract($result, EXTR_OVERWRITE);
echo $SENDERID;
echo $RECIPIENTID;
Demo
Compare Positions of Values in Php Array
Here is a demonstration with a battery of test cases to fully express how it works and identify fringe cases: (Demo Link)
*note, array_search()
returns false
when the needle is not found.
$commands = array("mark", "ignore", "pick", "random");
$attempts = array("mark", "ignore", "pick", "random", "bonk");
foreach($attempts as $attempt){
echo "$attempt => ";
$index=array_search($attempt,$commands);
// vv---increment the value
if($index===false || !isset($commands[++$index])){ // not found or found last element
$index=0; // use first element
}
echo $commands[$index],"\n";
}
The "or" (||
) condition will "short circuit", so if $index
is false
it will exit the condition without calling the second expression (isset()
).
Output:
mark => ignore
ignore => pick
pick => random
random => mark
bonk => mark
PHP compare arrays and remove NOT matched objects
array_intersect_key is what you're looking for. It takes two or more arrays, and returns a new array containing the elements of the first array, whose keys are present in all provided arrays.
Related Topics
How to Iterate Over Non-English File Names in PHP
PHP Fatal Error: Call to Undefined Function MySQLi_Stmt_Get_Result()
Set Session Variable Using JavaScript in PHP
How to Throttle My Site's API Users
How to Create Static Classes in PHP (Like in C#)
How to Easily Switch Between PHP Versions on MAC Osx
How to Bind Multiple Parameters to MySQLi Query
How to Get Int Instead String from Form
Class 'App\Http\Controllers\Admin\Auth' Not Found in Laravel 5
Warning: MySQLi_Query() Expects Parameter 1 to Be MySQLi, String Given In
Passing JavaScript Variable to PHP Using Ajax
Http Options Request on Azure Websites Fails Due to Cors
Strange Behaviour with Numbers That Have a Leading Zero
PHP Syntax for Arrays: Different Behaviour Between PHP Versions
Issue with Laravel Rules & Regex (Or) Operator
"Strict Standards: Only Variables Should Be Passed by Reference" Error