Home Backend Development PHP Tutorial How to calculate the intersection between multiple arrays in PHP? (code example)

How to calculate the intersection between multiple arrays in PHP? (code example)

Apr 08, 2019 am 11:37 AM
php Array intersection

This built-in function of PHP array_intersect_key() is used to calculate the intersection of two or more arrays. This function differs from array_intersect() and array_intersect_assoc() in that it uses keys for comparison and returns the matching key element. This function prints only the elements of the first array whose key values ​​match the elements of all other arrays. (Recommended: "PHP Tutorial")

Syntax:

array array_intersect_key($array1, $array2, $array3, $array4...)
Copy after login

Parameters: array_intersect_key() The function accepts at least two array as parameter. It can take any number of arrays, greater than or equal to two arrays separated by commas (', ').

Return type: The function returns another array containing the elements of the first array that are present as parameters in all other arrays, whose key values ​​match each other. If there are no matching keys, an empty array is returned.

Example:

输入: $array1 = ("1" => "aakash", "2" => "rishav", "3" => "gaurav")
        $array2 = ("1" => "shyam", "2" => "rishi", "5" => "rishav")
        $array3 = ("1" => "aakash", "4" => "raghav", "2" => "ravi")
输出:
        Array
        (
          [1] => aakash
          [2] => rishav
        )
Copy after login

The following program demonstrates the array_intersect_key() function. In the following program example, we use array_intersect_key() to find the intersection between arrays.

<?php 
   
$array1 = array("1" => "aakash", "2" => "rishav", "3" => "gaurav"); 
$array2 = array("1" => "shyam", "2" => "rishi", "5" => "rishav"); 
$array3 = array("1" => "aakash", "4" => "raghav", "2" => "ravi"); 
  
print_r(array_intersect_key($array1, $array2, $array3));
Copy after login

Output:

Array
(
    [1] => aakash
    [2] => rishav
)
Copy after login

This article is an introduction to the method of calculating the intersection between multiple arrays in PHP. It is simple and easy to understand. I hope it will be helpful to friends in need!

The above is the detailed content of How to calculate the intersection between multiple arrays in PHP? (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles