Skip to content

Instantly share code, notes, and snippets.

@austbot
Created November 27, 2015 22:34
Show Gist options
  • Save austbot/9d066bdaa47be3e2064f to your computer and use it in GitHub Desktop.
Save austbot/9d066bdaa47be3e2064f to your computer and use it in GitHub Desktop.
Give two arrays(can be multi dimensional) that have the same structure of keys. Return a value of the two where array 2 raplaces the values of array 1 if the returned value of the specific callback is truthy.
function array_replace_uwalk($array1, $array2, $callbackArray)
{
$result = $array1;
array_walk($result, function (&$array1Item, $key) use ($array2, $callbackArray)
{
$array2Item = $array2[$key];
if (is_array($array1Item) && is_array($array1Item))
{
$array1Item = array_replace_uwalk($array1Item, $array2Item, $callbackArray);
}else{
if(array_key_exists($key, $callbackArray)){
$res = call_user_func($callbackArray[$key],$array1Item, $array2Item);
if($res) $array1Item = $res;
}
}
});
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment