PHP 8.3.27 Released!

Voting

: min(five, one)?
(Example: nine)

The Note You're Voting On

lincoln dot du dot j at gmail dot com
4 months ago
This is a cleaner and more modern approach (PHP 7.4+ syntax).

<?php

$array1
= ['blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4];
$array2 = ['green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8];

$result = array_intersect_ukey($array1, $array2, fn($a, $b) => $a <=> $b);

var_dump($result);

?>

Explanation:
The comparison function fn($a, $b) => $a <=> $b uses the spaceship operator directly inside the array_intersect_ukey call.

The result will contain entries from $array1 whose keys also exist in $array2.

Output:
php
Copy
Edit
array(2) {
["blue"]=>
int(1)
["green"]=>
int(3)
}

<< Back to user notes page

To Top