Voting

: eight minus seven?
(Example: nine)

The Note You're Voting On

Carlos Granados
8 years ago
Here's a neat little snippet for filtering a set of records based on a the value of a column:

<?php

function dictionaryFilterList(array $source, array $data, string $column) : array
{
$new = array_column($data, $column);
$keep = array_diff($new, $source);

return
array_intersect_key($data, $keep);
}

// Usage:

$users = [
[
'first_name' => 'Jed', 'last_name' => 'Lopez'],
[
'first_name' => 'Carlos', 'last_name' => 'Granados'],
[
'first_name' => 'Dirty', 'last_name' => 'Diana'],
[
'first_name' => 'John', 'last_name' => 'Williams'],
[
'first_name' => 'Betty', 'last_name' => 'Boop'],
[
'first_name' => 'Dan', 'last_name' => 'Daniels'],
[
'first_name' => 'Britt', 'last_name' => 'Anderson'],
[
'first_name' => 'Will', 'last_name' => 'Smith'],
[
'first_name' => 'Magic', 'last_name' => 'Johnson'],
];

var_dump(dictionaryFilterList(['Dirty', 'Dan'], $users, 'first_name'));

// Outputs:
[
[
'first_name' => 'Jed', 'last_name' => 'Lopez'],
[
'first_name' => 'Carlos', 'last_name' => 'Granados'],
[
'first_name' => 'John', 'last_name' => 'Williams'],
[
'first_name' => 'Betty', 'last_name' => 'Boop'],
[
'first_name' => 'Britt', 'last_name' => 'Anderson'],
[
'first_name' => 'Will', 'last_name' => 'Smith'],
[
'first_name' => 'Magic', 'last_name' => 'Johnson']
]

?>

<< Back to user notes page

To Top