2017-05-25 10 views
0

はどのように、再帰的に1列、 このような場合に新しい要素を追加する することは、再帰的に

$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が役に立つことを願っています

解決方法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を使用するので、私はなぜよりエレガントな方法が必要なので、人々が私の質問に投票する理由を知りません。 –