Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将新元素递归地添加到一个数组中_Php_Arrays - Fatal编程技术网

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);