0

I want to merges all the values from identification,general,address,employer array to the single line of $aa array as below format.

the original array to merges.

  $aa = [
        0 => [
            'identification' => [
                1 => ['Iden_type' => 'types'],
                2 => ['Iden_num' => '000215'],
            ],
            'general' => [
                1 => ['gen_name' => 'name'],
                2 => ['gen_lname' => 'lname'],
            ],
            'address' => [
                1 => ['add_type' => 'type'],
                2 => ['add_text' => 'text'],
            ],
            'contact' => [
                1 => ['cont_type' => 'types'],
                2 => ['cont_text' => 'text'],
            ],
            'employer' => [
                1 => ['emp_fname' => 'first name'],
                2 => ['emp_lname' => 'last name'],
            ],

        ]
    ];

Result that I want to get from above array.

 $aa = [
        0 => [
            'types',
            '000215',
            'name',
            'lname',
            'type',
            'text',
            'types',
            'text',
            'first name',
            'last name',

        ],
        1 => [
            'types',
            '000215',
            'name',
            'lname',
            'type',
            'text',
            'types',
            'text',
            'first name',
            'last name',

        ],

    ];

1 Answer 1

1
foreach ($aa as $k => $v) {
    $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($v));
    foreach ($it as $value) {
        $result[$k][] = $value;
    }
}

SEE WORKING DEMO

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

3 Comments

Class 'App\Http\Controllers\RecursiveIteratorIterator' not found
Try use new \RecursiveIteratorIterator instead
An instance of RecursiveIterator or IteratorAggregate creating it is required

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.