2

Whats the easiest way to convert array a to b

a= [['x'=>'a'], ['y'=>'b']] 


b= ['x'=>'a', 'y'=>'b']

a and b are just two examples.

3
  • 1
    $b = array_merge (...$a); Commented May 25, 2017 at 19:17
  • A more simple solution using RecursiveIteratorIterator is present here: stackoverflow.com/questions/1319903/… Commented May 25, 2017 at 19:21
  • @meagar how is this question a duplicate? Similar solutions don't mean that it is a duplicate question. Commented May 25, 2017 at 19:28

1 Answer 1

0

Using array_walk_recursive for arbitrary depth:

$b = [];
array_walk_recursive($a, function ($v, $k) use (&$b) { $new[$k] = $v; });

Using @splash58 trick with the spread operator if you have only one level deep:

$b = array_merge(...$a);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.