我有这个数组…
[
[
[
"name" => "Initials"
"value" => "ROR"
],
[
"name" => "Awards"
"value" => "Swimming"
],
],
[
[
"name" => "Initials"
"value" => "NAL"
],
[
"name" => "Awards"
"value" => "Cycling"
],
]
]
对我来说,改变这个数组的最好方法是什么?我可以这样保存它…
[
[
"Initials" => "ROR"
"Awards" => "Swimming"
],
[
"Initials" => "NAL"
"Awards" => "Cycling"
],
]
我已经开始做一些事情,比如循环遍历它们和这个…但这给了我一个数组元素,每个新数组
foreach($array as $rows) {
foreach($rows as $field) {
$values[] = [$field['name'] => $field['value']];
}
}
您可以使用array_column()
来索引内部数组,指定要使用的列作为值,并指定要索引的列…
$values = [];
foreach($array as $rows) {
$values[] = array_column($rows, "value", "name");
}
print_r($values);
给了…
Array
(
[0] => Array
(
[Initials] => ROR
[Awards] => Swimming
)
[1] => Array
(
[Initials] => NAL
[Awards] => Cycling
)
)
你可以这么做
foreach($array as $rows) {
foreach($rows as $field) {
$values[] = [$field];
}
}
或者直接用
$newArray = $array[0];