1

我有一个这样的数组

$category = [
'cat1' => [
    'images/category/cat1/icon1.jpg',
    'images/category/cat1/icon2.jpg',
    'images/category/cat1/icon3.jpg',
],
'cat2' => [
    'images/category/cat2/icon1.jpg',
    'images/category/cat2/icon2.jpg',
    'images/category/cat2/icon3.jpg',
]
];

我想得到这样的数组

$categories = [
'images/category/cat1/icon1.jpg',
'images/category/cat1/icon2.jpg',
'images/category/cat1/icon3.jpg',
'images/category/cat2/icon1.jpg',
'images/category/cat2/icon2.jpg',
'images/category/cat2/icon3.jpg',
];

我试过了,但没有得到预期的输出。

$categories = array_map(function($cat){return $cat;},$category);
4

2 回答 2

4

您可以call_user_func_array结合使用array_merge

$category = call_user_func_array('array_merge', $category);
于 2014-12-18T02:24:57.410 回答
1

可以通过array_reduce()&解决array_merge()

$categories = array_reduce($category, function($old, $new){
    return array_merge($old, $new);
}, array());
于 2014-12-18T02:25:13.860 回答