Php 将新元素递归地添加到一个数组中
如何将新元素递归地添加到一个数组中, 像这样的情况,Php 将新元素递归地添加到一个数组中,php,arrays,Php,Arrays,如何将新元素递归地添加到一个数组中, 像这样的情况, $insertNew = "Another Value"; 主阵列: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) 我需要一个如下的数组,因为我想在mysql中创建一个插入批处理 [ ['Another Value', 1], ['Another Value', 2],
$insertNew = "Another Value";
主阵列:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
我需要一个如下的数组,因为我想在mysql中创建一个插入批处理
[
['Another Value', 1],
['Another Value', 2],
['Another Value', 3],
['Another Value', 4],
]
请提供建议。希望这个最简单的建议会有所帮助 解决方案1: 解决方案2:
谢谢,但我想这句话太多了。我一直都这样做,我使用forecah,我不知道为什么人们会反对我的问题,因为我需要更优雅的方式。
$result=array();
$insertNew = "Another Value";
foreach($yourArray as $value)
{
$result[]=array($insertNew,$value);
}
print_r($result);
$insertNew = "Another Value";
$result= array_map(function($value) use ($insertNew){
return array($insertNew,$value);
}, $array);
print_r($result);