How to Compare Two Arrays for Matching Values in PHP

1 year ago admin PHP

In this lesson, we are going to see how to compare two arrays and find the matching values in PHP, we can do that using the array_intersect() function.


PHP array_intersect() Function

To accomplish this as we said we use the array_intersect() function.

                                                    
                                                                                                                
<?php

 $males = [
      "name" => "John Doe",
      "age" => 34,
      "city" => "Paris"
 ];
 $females = [
      "name" => "Maria Maria",
      "age" => 39,
      "city" => "Paris"
 ];

 $result = array_intersect($males, $females);

 print_r($result);
 
 //result
 Array ( [city] => Paris )

Related Tuorials

CRUD Application with PHP PDO Ajax, and MySQL Part 2

In the second part of this tutorial, we will get all the students from the database and display them...


CRUD Application with PHP PDO Ajax, and MySQL Part 1

In this tutorial we will see how to create a crud application with PHP PDO Ajax, and MySQL, the user...


How to Sort Associative Arrays in Descending Order According to the Key Value in PHP

in this lesson, we will see how to sort associative arrays in descending order according to the key...


How to Sort Associative Arrays in Ascending Order According to the Key Value in PHP

in this lesson, we will see how to sort associative arrays in ascending order according to the key v...


How to Sort Associative Arrays in Descending Order According to the Value in PHP

in this lesson, we will see how to sort associative arrays in descending order according to the valu...


How to Sort Associative Arrays in Ascending Order According to the Value in PHP

in this lesson, we will see how to sort associative arrays in ascending order according to the value...


How Do you Sort an Array in Descending Order in PHP

In this lesson, we will see how to sort descending an array in PHP, we will use the rsort() function...


How to Sort Ascending an Array in PHP

In this lesson, we will see how to sort ascending an array in PHP, we will use the sort() function t...


How to Remove a Key and its Value from an Associative Array in PHP

In this lesson, we will see how to remove a key and its value from an associative array in PHP,&nbsp...


How to Modify a Value in an Associative Array in PHP

In this lesson, we will see how to modify a value in an associative array in PHP, an Associative arr...