Skip to content

Instantly share code, notes, and snippets.

@benklocek
Last active December 16, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benklocek/5446492 to your computer and use it in GitHub Desktop.
Save benklocek/5446492 to your computer and use it in GitHub Desktop.
<?php
//Goal to merge $user['CustomFields'] with $customfields.
//fields from API
$customfields[] = (object) array('Key' => 'Email Lists', 'Value' => 'Events');
$customfields[] = (object) array('Key' => 'Office Affiliation', 'Value' => 'Oxford');
$customfields[] = (object) array('Key' => 'Office Affiliation', 'Value' => 'San Francisco');
//fields from form
$user['CustomFields'][] = array('Key' => 'Email Lists', 'Value' => 'Newsletter');
$user['CustomFields'][] = array('Key' => 'Email Lists', 'Value' => 'Events');
$user['CustomFields'][] = array('Key' => 'Office Affiliation', 'Value' => 'Oxford');
echo '<pre>';
print_r($customfields);
print_r($user['CustomFields']);
echo '</pre>';
foreach( $customfields as $customfield ){
//echo $customfield->Value .'<br>';
//find matches in array
if(!in_array_recursive($customfield, $user['CustomFields']))
$user['CustomFields'][] = array('Key' => $customfield->Key, 'Value' => $customfield->Value);
}
echo '<pre>Updated Version
';
print_r($user['CustomFields']);
echo '</pre>';
function in_array_recursive($needle, $haystack) {
foreach ($haystack as $element) {
if ((object) $element == (object) $needle) return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment