1

I have a multidimensional array with an unknown structure and unknown number of items. I need to get a "flater" array compiled from particular keys, like in following example:

Input

$input = array(

    1 => array(
         'color' => 'blue',
         'haircut' => 'mullet',
         'satisfaction' => 'mild',
         'recursion' => array(...)
    ),

    2 => array(
         'color' => 'green',
         'haircut' => 'mushroom',
         'fashionable' => true,
         'recursion' => array(...)
    ),

    3 => array(
         'color' => 'yellow',
         'haircut' => 'beehive',
         'awkward' => false,
         'recursion' => array(...)
    ),
    
    ...
     
);

Output (merged array from above)

$output = array(
    'color' => 'yellow',
    'haircut' => 'beehive',
    'satisfaction' => 'mild',
    'fashionable' => true,
    'awkward' => false,
    'recursion' => array(...)
);

The ideal way, I think, would be to use array_replace_recursive($input[0],$input[1],$input[2], ...), right? The problem is I don't know the exact number of keys in $input array.

Is there any easy way how to achieve this? My guess would be to use each() function, but to be honest, I never really got that one.

Can you unwrap my head a little bit? Thank you in advance!

1 Answer 1

1

This is probably what you need:

$output = call_user_func_array('array_replace', $input);

It calls array_replace() whereby each element of $input is passed in as a separate function argument.

Sign up to request clarification or add additional context in comments.

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.