PHP 如何组合数组和创建新数组
PHP how to combine array and create new array
我有 3 个这样的数组:
$first = array(
[0] => 13
[1] => 66
[2] => 15
)
$second = array
(
[0] => append
[1] => prepend
[2] => append
)
$third = array
(
[0] => 2
[1] => 4
[2] => 1
)
现在我想合并这 3 个数组并创建新的 SINGLE 数组,如下所示:
我想从每个数组中获取值并将其合并为一个。
$new_array = array(
[0] = array (
'page'=>13,
'position'=>'append'
'priority'=>'2'
)
[1] = array (
'page'=>66,
'position'=>'prepend'
'priority'=>'4'
)
[2] = array (
'page'=>15,
'position'=>'append'
'priority'=>'1'
)
)
如何操作?
只需映射每个数组的每个元素和 return 一个包含以下键的数组:
$result = array_map(function($f, $s, $t) {
return ['page'=>$f, 'position'=>$s, 'priority'=>$t];
}, $first, $second, $third);
或者在函数外定义键并与每个元素组合:
$keys = ['page', 'position', 'priority'];
$result = array_map(function($f, $s, $t) use($keys) {
return array_combine($keys, [$f, $s, $t]);
}, $first, $second, $third);
您可以使用array_map()遍历多个数组(相同长度)并从中逐项构建一个新数组:
$first = array(13, 66, 15);
$second = array('append', 'prepend', 'append');
$third = array(2, 4, 1);
print_r(
array_map(
function($page, $position, $priority) {
return compact('page', 'position', 'priority');
},
$first,
$second,
$third
)
);
虽然在这种情况下,使用简单的结构 foreach() 可能是替代方案。
编辑: 版本 2。有些人可能会争辩说这更糟糕,因为可读性和代码可维护性。感谢 AbraCadaver
$keys = ['page', 'position', 'priority'];
// Note: The order in $keys and array_map() arguments must be the same
// On the other hand you only need to type the keys once and it's easy
// to change the number of arguments :)
print_r(
array_map(
function(...$args) use ($keys) {
return array_combine($keys, $args);
},
$first,
$second,
$third
)
);
编辑: 版本 3。使用带有箭头功能的稍微更现代的 PHP (7.4):
print_r(
array_map(
fn(...$args) => array_combine($keys, $args),
$first,
$second,
$third
)
);
如果你有相同的列,你可以这样做
$first = [13,66,15];
$second = ['append','prepend','append'];
$third = [2,4,1];
foreach($first as $v => $i){
$new_array[] = [
'page' => $i,
'position' => $second[$v],
'priority' => $third[$v],
];
}
print_r($new_array);
这是结果
我有 3 个这样的数组:
$first = array(
[0] => 13
[1] => 66
[2] => 15
)
$second = array
(
[0] => append
[1] => prepend
[2] => append
)
$third = array
(
[0] => 2
[1] => 4
[2] => 1
)
现在我想合并这 3 个数组并创建新的 SINGLE 数组,如下所示: 我想从每个数组中获取值并将其合并为一个。
$new_array = array(
[0] = array (
'page'=>13,
'position'=>'append'
'priority'=>'2'
)
[1] = array (
'page'=>66,
'position'=>'prepend'
'priority'=>'4'
)
[2] = array (
'page'=>15,
'position'=>'append'
'priority'=>'1'
)
)
如何操作?
只需映射每个数组的每个元素和 return 一个包含以下键的数组:
$result = array_map(function($f, $s, $t) {
return ['page'=>$f, 'position'=>$s, 'priority'=>$t];
}, $first, $second, $third);
或者在函数外定义键并与每个元素组合:
$keys = ['page', 'position', 'priority'];
$result = array_map(function($f, $s, $t) use($keys) {
return array_combine($keys, [$f, $s, $t]);
}, $first, $second, $third);
您可以使用array_map()遍历多个数组(相同长度)并从中逐项构建一个新数组:
$first = array(13, 66, 15);
$second = array('append', 'prepend', 'append');
$third = array(2, 4, 1);
print_r(
array_map(
function($page, $position, $priority) {
return compact('page', 'position', 'priority');
},
$first,
$second,
$third
)
);
虽然在这种情况下,使用简单的结构 foreach() 可能是替代方案。
编辑: 版本 2。有些人可能会争辩说这更糟糕,因为可读性和代码可维护性。感谢 AbraCadaver
$keys = ['page', 'position', 'priority'];
// Note: The order in $keys and array_map() arguments must be the same
// On the other hand you only need to type the keys once and it's easy
// to change the number of arguments :)
print_r(
array_map(
function(...$args) use ($keys) {
return array_combine($keys, $args);
},
$first,
$second,
$third
)
);
编辑: 版本 3。使用带有箭头功能的稍微更现代的 PHP (7.4):
print_r(
array_map(
fn(...$args) => array_combine($keys, $args),
$first,
$second,
$third
)
);
如果你有相同的列,你可以这样做
$first = [13,66,15];
$second = ['append','prepend','append'];
$third = [2,4,1];
foreach($first as $v => $i){
$new_array[] = [
'page' => $i,
'position' => $second[$v],
'priority' => $third[$v],
];
}
print_r($new_array);
这是结果