如何将数组的数组映射到单个关联数组?

时间:2018-05-23 09:26:53

标签: php arrays laravel php-7

我有:

subscribe

我试图得到:

array:2 [
  0 => array:1 [
    "FNAME" => "nullable|string"
  ]
  1 => array:1 [
    "LNAME" => "nullable|string"
  ]
]

我尝试映射它,但有问题

2 个答案:

答案 0 :(得分:2)

<?php
$array = [
    [
        "FNAME" => "nullable|string",
    ],
    [
        "LNAME" => "nullable|string",
    ]
];

$newArray = [];

foreach ($array as $item) {
    foreach ($item as $key => $value) {
        $newArray[$key] = $value;
    }
}

print_r($newArray);

将输出:

Array
(
    [FNAME] => nullable|string
    [LNAME] => nullable|string
)

答案 1 :(得分:1)

两种简单的方式:

print_r(array_merge(...$arr));
// if `...` is not available (php < 5.6), then:
print_r(call_user_func_array('array_merge', $arr))