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)
Explore
Basics
Array
OOPs & Interfaces
MySQL Database
PHP Advance