0

I'm trying to merge a collection of nested array elements into one array.

Array:

crop_data = [
                [
                    ["crop" => "soy"]   // 0
                ],

                [
                    ["crop" => "rye"]   // 1
                ],

                [
                    ["crop" => "tree"]  // 2
                ]
            ],

            [
                [
                    ["crop" => "salt"]  // 0
                ],

                [
                    ["crop" => "farm"]  // 1
                ]
            ],

            [
                [
                    ["year" => "2015"]
                ]
            ]

I've tried the following...

$crop_data = array();   // new array

foreach($crop_list as $value) {
    $crop_data = array_merge($value, $crop_list));
}

I would like to merge the inner elements of the three arrays into one array. Any tips on how to achieve this?

2
  • 1) Have you tried something ? 2) What is your expected output? Commented Jul 27, 2015 at 19:52
  • 1
    writing code would would be a good start... Commented Jul 27, 2015 at 19:52

1 Answer 1

1

You can use array_walk_recursive for this.

$merged = array();
array_walk_recursive($crop_data, function($v, $k) use (&$merged) {
    $merged[$k][] = $v;
});
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.