我有多维数组,我希望它在一个变量
中分成多个数组Array
(
[name] => Array
(
[0] => John Davis
[1] => Marie J
)
[work] => Array
(
[0] => employee
[1] => none
)
[address] => Array
(
[0] => street 1
[1] => street 2
)
)
我希望输出到:
$array1 = array("name" => "John Davis", "work" => "employee", "address" => "street 1");
$array2 = array("name" => "Marie J", "work" => "none", "address" => "street 2");
以及如果值键超过2
,如何动态生成非常感谢
答案 0 :(得分:3)
请尝试以下代码获取输出:
$newArray = [];
foreach ($outerArray as $outerkey => $outerArr) {
foreach ($outerArr as $key => $innerArr) {
$newArray[$key][$outerkey] = $innerArr;
}
}
print_r($newArray);
答案 1 :(得分:1)
功能解决方案
// 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));