2017-05-25 69 views
0

如何添加一个新的元素到一个数组递归, 这样的情况下,添加一个新的元素到一个数组递归

$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

希望这简单的一个将有助于

解决方案1:

Try this code snippet here

$result=array(); 
$insertNew = "Another Value"; 
foreach($yourArray as $value) 
{ 
    $result[]=array($insertNew,$value); 
} 
print_r($result); 

解决方案2:

Try this code snippet here

$insertNew = "Another Value"; 
$result= array_map(function($value) use ($insertNew){ 
    return array($insertNew,$value); 
}, $array); 

print_r($result); 
+0

谢谢,但我猜太多了。我一直这样做,我用forecah然后如此,我不知道为什么人们投我的问题,因为需要更优雅的方式。 –

相关问题