Ravi Shakya
Ravi Shakya

Reputation: 19

Remove the parent keys from the multidimentional array

I have a multidimensional array. I want to remove the parent keys [0],[1],[2] ..... What's the best way to remove the parent keys in an Multidimensional Array?. For example I have this code

Array
(
[0] => Array
    (
        [comment_tN9l43iUjZLNap4Dbkf7w8Whb3] => Array
            (
                [required] => This field is required
            )

    )

[1] => Array
    (
        [checkbox_cNVyw1lV0eVrYdeymth2c90AW] => Array
            (
                [required] => Select Gender
                [minlength] => Please select at least 2 items.
                [maxlength] => Please select no more than 4 items.
            )

    )

[2] => Array
    (
        [checkbox_EM9tkQoZ4YMaPncAPenfi6ltB] => Array
            (
                [required] => This field is required
                [minlength] => Please select at least 1 items.
                [maxlength] => Please select no more than 3 items.
            )

    )

)

but i want the array like this

Array
    (
        [comment_tN9l43iUjZLNap4Dbkf7w8Whb3] => Array
            (
                [required] => This field is required
            )
        [checkbox_cNVyw1lV0eVrYdeymth2c90AW] => Array
            (
                [required] => Select Gender
                [minlength] => Please select at least 2 items.
                [maxlength] => Please select no more than 4 items.
            )
        [checkbox_EM9tkQoZ4YMaPncAPenfi6ltB] => Array
            (
                [required] => This field is required
                [minlength] => Please select at least 1 items.
                [maxlength] => Please select no more than 3 items.
            )

    )

Upvotes: 0

Views: 909

Answers (2)

user5147376
user5147376

Reputation:

You may try this:

$newArray = [];
foreach ($oldArray AS $data) {
    $newArray = array_merge($newArray, $data);
}

See the documentation for array_merge() for more information.

Upvotes: 0

Orangepill
Orangepill

Reputation: 24645

It's easy all you have to do is call array_merge via call_user_func_array to get the results you want.

$newArray = call_user_func_array("array_merge", $oldArray);

You can see a simplified example here

Upvotes: 4

Related Questions