3

I got multidimensional arrays i want it to break into multi array in one variable

Array
(
    [name] => Array
        (
            [0] => John Davis
            [1] => Marie J
        )

    [work] => Array
        (
            [0] => employee
            [1] => none
        )

    [address] => Array
        (
            [0] => street 1
            [1] => street 2
        )

)

I want output to :

$array1 = array("name" => "John Davis", "work" => "employee", "address" => "street 1");
$array2 = array("name" => "Marie J", "work" => "none", "address" => "street 2");

and how to generate dynamically if values keys more than 2

thanks alot

4

2 Answers 2

3

Please try below code get output:

$newArray = [];
foreach ($outerArray as $outerkey => $outerArr) {
    foreach ($outerArr as $key => $innerArr) {
        $newArray[$key][$outerkey] = $innerArr;
    }
}

print_r($newArray);
Sign up to request clarification or add additional context in comments.

Comments

1

Functional solution

// make array of keys
$keys = array_keys($arr);
// combine it with data sets 
$res = array_map(function (...$x) use($keys) 
       { return array_combine($keys, $x); },
       ...array_values($arr));

demo

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.