This is a simple way to get a subset of associative array elements using an array of keys; basically you extract out the elements you want from a bigger array by listing the keys you need.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$associative_array = ['firstname' => 'John', 'lastname' => 'Smith', 'DOB' => '2000-10-10', 'country' => 'Ireland' ]; | |
$subset = array_intersect_key( $associative_array, array_flip( [ 'lastname', 'country' ] ) ); | |
print_r( $subset ); | |
// Outputs… | |
// Array ( [lastname] => Smith [country] => Ireland ); |
I have come by this before when searching an associative array using a wildcard.