我有一个这样的数组(为清楚起见进行了抽象):
$foo = array(
'breakfast' => 'a daily meal',
'lunch' => 'a daily meal',
'dessert' => 'a special treat',
'snack' => 'a special treat',
'plates' => 'tableware'
);
为此,我想将所有重复值的键组合成一个“合并”键 -print_r($foo);
就像这样:
Array
(
[breakfast|lunch] => 'a daily meal'
[dessert|snack] => 'a special treat'
[plates] => 'tableware'
)
目前我正在通过一长串嵌套的丑陋 foreach 语句来实现这一点……有没有更简单/更合适的方法?