Skip to content

Instantly share code, notes, and snippets.

@jamesBan
Last active March 15, 2016 05:36
Show Gist options
  • Select an option

  • Save jamesBan/b03ca2b3755e3a7691c4 to your computer and use it in GitHub Desktop.

Select an option

Save jamesBan/b03ca2b3755e3a7691c4 to your computer and use it in GitHub Desktop.
嵌套匹配
<?php
$a = [
'1,2',
'3',
'4',
'5,6'
];
$hash = [
1 => 'xxx1',
2 => 'xxx2',
3 => 'xxx3',
4 => 'xxx4',
5 => 'xxx5',
6 => 'xxx6',
];
foreach ($a as $key => $id) {
$idArray = explode(',', $id);
$nickname = '';
$name = array_reduce($idArray, function($nickname, $id) use($hash){
$tmp = isset($hash[$id]) ? $hash[$id] :"";
return $nickname = $nickname .','.$tmp;
}, '');
echo $id, "|", trim($name, ',');
echo PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment