Skip to content

Instantly share code, notes, and snippets.

@atbradley
Last active December 16, 2015 17:09
Show Gist options
  • Save atbradley/5468340 to your computer and use it in GitHub Desktop.
Save atbradley/5468340 to your computer and use it in GitHub Desktop.
Ensure that an associative array contains values for all required keys and only those keys. (Basically the PHP5 equivalent of Textpattern's lAtts() function. It's one of those things I'm sick of having to figure out every time I need it.)
<?php
$defaults=array('a'=>1, 'b'=>2, 'c'=>3);
$input=array('a'=> 'boo', 'c'=>4, 'd'=>5, 'e'=>6);
$output = array_intersect_key($input+$defaults, $defaults);
//Might want to provide a warning if $unused isn't empty.
$unused = array_diff_key($input, $defaults);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment