PHP: Filter array by allowed key list

raw

0-code.php

1
2
3
4
5
6
7
8
9
10
11
12
<?php
$incomingData = [
    'allowed'     => 23,
    'alsoAllowed' => 42,
    'notAllowed'  => 'foo',
];
 
$allowedKeys = ['allowed', 'alsoAllowed'];
 
$validIncoming = array_intersect_key($incomingData, array_flip($allowedKeys));
var_dump($validIncoming);
 
raw

1-output.txt

array(2) {
  'allowed' =>
  int(23)
  'alsoAllowed' =>
  int(42)
}
Christian Weiske Christian Weiske
owner

History