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);
3

2 Answers 2

4

You can use call_user_func_array in conjuction with array_merge:

$category = call_user_func_array('array_merge', $category);
Sign up to request clarification or add additional context in comments.

Comments

1

Can be solve by array_reduce() & array_merge()

$categories = array_reduce($category, function($old, $new){
    return array_merge($old, $new);
}, array());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.