phparraysmultidimensional-arrayassociative-arrayflatten

Flatten a multidimensional associative array by one level


I don't know how to write a nested for loop to convert into a key value array.

This is the array structure.

I need to flatten the second level data to become the first level (combining JobDescription and userdetail array together).

array(2) {
    ["jobDescription"]=> array(5) {
        ["funeralFor"]=> string(6) "Myself"
        ["serviceType"]=> string(1) "1"
        ["religionType"]=> string(1) "2"
        ["area"]=> string(4) "2154"
        ["customerComment"]=> string(6) "fdfddf"
    }
    ["userDetail"]=> array(6) {
        ["contactEmail"]=> string(16) "fdddf@fffgfg.com"
        ["contactFirstName"]=> string(6) "fddfdf"
        ["contactLastName"]=> string(6) "fddffd"
        ["contactPhoneNumber"]=> string(10) "0420988191"
        ["signup"]=> array(2) {
            ["id"]=> string(32) "8048f0f7106c336e1a8825d1d3bec902"
            ["input"]=> string(3) "k5m"
        }
        ["agreement"]=> string(1) "1"
    }
}

Solution

  • You have two arrays stored in an array. You want the values of both arrays under one array instead of two subarrays?

    $newArray = array_merge($array['jobDescription'], $array['userDetail']);