Skip to content

Instantly share code, notes, and snippets.

@kresnasatya
Created May 3, 2018 03:36
Show Gist options
  • Save kresnasatya/20637105adfd2727a1a778fb8d295bf9 to your computer and use it in GitHub Desktop.
Save kresnasatya/20637105adfd2727a1a778fb8d295bf9 to your computer and use it in GitHub Desktop.
Combine key with arrays
<?php
function combine_keys_with_arrays($keys, $arrays) {
$results = array();
foreach ($arrays as $subKey => $arr)
{
foreach ($keys as $index => $key)
{
$results[$key][$subKey] = $arr[$index];
}
}
return $results;
}
$keys = array('u1', 'u2', 'u3');
$names = array('Bob', 'Fred', 'Joe');
$emails = array('[email protected]', '[email protected]', '[email protected]');
$ids = array(1,2,3);
$result = combine_keys_with_arrays($keys, array('name' => $names,
'email' => $emails,
'id' => $ids));
echo"<pre>";print_r($result);echo"</pre>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment