1

I have an array like this

$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',
]
];

I want to get the array like this

$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',
];

I tried this but not got expected output.

$categories = array_map(function($cat){return $cat;},$category);
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • You're more likely to get help if you post the actual output you received. (But thanks for showing us what output you want and what you tried.) – Jeffrey Bosboom Dec 18 '14 at 02:23
  • possible duplicate of [How to "flatten" a multi-dimensional array to simple one in PHP?](http://stackoverflow.com/questions/526556/how-to-flatten-a-multi-dimensional-array-to-simple-one-in-php) – Prasanth Bendra Dec 18 '14 at 13:22
  • http://stackoverflow.com/questions/526556/how-to-flatten-a-multi-dimensional-array-to-simple-one-in-php/14972714#14972714 – Prasanth Bendra Dec 18 '14 at 13:22

2 Answers2

4

You can use call_user_func_array in conjuction with array_merge:

$category = call_user_func_array('array_merge', $category);
Kevin
  • 41,694
  • 12
  • 53
  • 70
1

Can be solve by array_reduce() & array_merge()

$categories = array_reduce($category, function($old, $new){
    return array_merge($old, $new);
}, array());
MH2K9
  • 11,951
  • 7
  • 32
  • 49