How to Find Matching Pair from an Array in PHP ?
Last Updated :
16 Jul, 2024
Given an Array, the task is to find the matching pair from a given array in PHP. Matching a pair from an array involves searching for two elements that satisfy a certain condition, such as having the same value, summing up to a specific target, or meeting some other criteria. Here, we will cover three common scenarios for finding matching pairs in an array.
Finding Pairs with a Specific Sum
To find pairs in an array that add to a specific sum, you can use a nested loop to iterate through the array and check for pairs.
Example:
PHP
<?php
function pairSum($arr, $targetSum)
{
$pairs = [];
$len = count($arr);
for ($i = 0; $i < $len - 1; $i++) {
for ($j = $i + 1; $j < $len; $j++) {
if ($arr[$i] + $arr[$j] == $targetSum) {
$pairs[] = [$arr[$i], $arr[$j]];
}
}
}
return $pairs;
}
$arr = [2, 7, 4, 5, 11, 15];
$sum = 9;
$result = pairSum($arr, $sum);
print_r($result);
?>
OutputArray
(
[0] => Array
(
[0] => 2
[1] => 7
)
[1] => Array
(
[0] => 4
[1] => 5
)
)
Finding Duplicate Pairs
To find the duplicate pairs (pairs with the same values), you can use nested loops to compare each element with every other element in the array.
Example:
PHP
<?php
function duplicatePairs($arr) {
$pairs = [];
$len = count($arr);
for ($i = 0; $i < $len - 1; $i++) {
for ($j = $i + 1; $j < $len; $j++) {
if ($arr[$i] == $arr[$j]) {
$pairs[] = [$arr[$i], $arr[$j]];
}
}
}
return $pairs;
}
// Driver code
$arr = [1, 2, 3, 2, 4, 1];
$res = duplicatePairs($arr);
print_r($res);
?>
OutputArray
(
[0] => Array
(
[0] => 1
[1] => 1
)
[1] => Array
(
[0] => 2
[1] => 2
)
)
Finding Pairs with a Specific Value
To find pairs with a specific value, you can iterate through the array and check for elements that match the desired value.
Example:
PHP
<?php
function pairsWithValue($arr, $targetValue)
{
$pairs = [];
foreach ($arr as $value) {
if (in_array($targetValue - $value, $arr)) {
$pairs[] = [$value, $targetValue - $value];
}
}
return $pairs;
}
$arr = [3, 1, 4, 6, 5, 2];
$targetVal = 7;
$res = pairsWithValue($arr, $targetVal);
print_r($res);
?>
OutputArray
(
[0] => Array
(
[0] => 3
[1] => 4
)
[1] => Array
(
[0] => 1
[1] => 6
)
[2] => Array
(
...
Using a Hash Map to Find Pairs with a Specific Sum
This method involves using an associative array (hash map) to efficiently find pairs in an array that sum to a specific target. The hash map is used to store the difference between the target sum and each element as keys, and their corresponding indices as values. This allows you to check if the current element's complement (i.e., the number needed to reach the target sum) already exists in the hash map as a key.
Example: Here’s how you can implement this method to find pairs in an array that add up to a specific sum:
PHP
<?php
$numbers = [1, 2, 3];
$targetSum = 4;
$hash = [];
$pairs = [];
foreach ($numbers as $index => $num) {
$complement = $targetSum - $num;
if (isset($hash[$complement])) {
$pairs[] = [$complement, $num];
}
$hash[$num] = $index;
}
print_r($pairs);
?>
OutputArray
(
[0] => Array
(
[0] => 1
[1] => 3
)
)
Finding Pairs with a Specific Product
To find pairs in an array that multiply to a specific product, you can use a nested loop to iterate through the array and check for pairs whose product equals the target product.
Example:
PHP
<?php
function pairProduct($arr, $targetProduct) {
$pairs = [];
$len = count($arr);
for ($i = 0; $i < $len - 1; $i++) {
for ($j = $i + 1; $j < $len; $j++) {
if ($arr[$i] * $arr[$j] == $targetProduct) {
$pairs[] = [$arr[$i], $arr[$j]];
}
}
}
return $pairs;
}
// Driver code
$arr = [2, 4, 1, 6, 5, 10, 8];
$product = 20;
$result = pairProduct($arr, $product);
print_r($result);
?>
OutputArray
(
[0] => Array
(
[0] => 2
[1] => 10
)
[1] => Array
(
[0] => 4
[1] => 5
)
)
Finding Pairs with an Even Sum
To find pairs in an array where the sum of the two elements is even, you can use a nested loop to iterate through the array and check if the sum of any two elements is even.
Example
PHP
<?php
function findPairsWithEvenSum($array) {
$result = [];
$n = count($array);
for ($i = 0; $i < $n; $i++) {
for ($j = $i + 1; $j < $n; $j++) {
if (($array[$i] + $array[$j]) % 2 == 0) {
$result[] = [$array[$i], $array[$j]];
}
}
}
return $result;
}
$array = [1, 2, 3, 4, 5];
$pairs = findPairsWithEvenSum($array);
foreach ($pairs as $pair) {
echo "Pair with even sum: (" . $pair[0] . ", " . $pair[1] . ")\n";
}
?>
OutputPair with even sum: (1, 3)
Pair with even sum: (1, 5)
Pair with even sum: (2, 4)
Pair with even sum: (3, 5)
Similar Reads
PHP | Find Intersection of two arrays You are given two arrays of n-elements each. You have to find all the common elements of both elements, without using any loop in php and print the resulting array of common elements. Example: Input : array1[] = {3, 5, 2, 7, 9}, array2[] = {4, 3, 2, 7, 8} Output : array ( [0] => 3, [1] => 2, [
2 min read
How to check an element is exists in array or not in PHP ? An array may contain elements belonging to different data types, integer, character, or logical type. The values can then be inspected in the array using various in-built methods : Approach 1 (Using in_array() method): The array() method can be used to declare an array. The in_array() method in PHP
2 min read
Find the Most Frequent Element in an Array in PHP Given an array, i.e. $arr, the task is to find the most frequent element in an array. There are multiple ways to solve this problem in PHP we will be going to discuss them. These problems can be used during data analysis, Web Analytics, or highly used in fraud detection. Example: Input: $array = [1,
2 min read
How to find the index of an element in an array using PHP ? In this article, we will discuss how to find the index of an element in an array in PHP. Array indexing starts from 0 to n-1. Here we have some common approachesTable of ContentUsing array_search() FunctionUsing array_flip()Using a custom functionUsing a foreach LoopUsing array_keys FunctionUsing ar
6 min read
How to Find Union and Intersection of Two Unsorted Arrays in PHP? Given two unsorted arrays representing two sets (elements in every array are distinct), find the union and intersection of two arrays in PHP. Example: Input: arr1[] = {7, 1, 5, 2, 3, 6} , arr2[] = {3, 8, 6, 20, 7} Output: Union {1, 2, 3, 5, 6, 7, 8, 20} and Intersection as {3, 6, 7}These are the fol
4 min read