Skip to content

Instantly share code, notes, and snippets.

@ScreamingDev
Last active December 15, 2015 15:19
Show Gist options
  • Save ScreamingDev/5281177 to your computer and use it in GitHub Desktop.
Save ScreamingDev/5281177 to your computer and use it in GitHub Desktop.
Exchange Array
<?php
$default = [
'one' => null,
'two' => null,
];
$incoming = ['two' => 123, 'three' => 345];
// only allow existing keys
$incoming = array_intersect_key($incoming, $default);
// fill up with defaults
$result = array_merge($default, $incoming);
// var_dump($result);
?>
<?php
public function exchangeArray($data)
{
// defaults
$default = array(
'foo' => null,
);
// only allow existing keys
$data = array_intersect_key($data, $default);
// fill up with defaults
$data = array_merge($default, $data);
// $this->data = $data;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment