逐行加入PHP中的两个变量

时间:2014-09-10 04:45:21

标签: php

我有两个变量。

例如:

Var 1

apple;orange;banana PHP_EOL
pear;orange;banana PHP_EOL
peach;orange;banana PHP_EOL
melon;orange;banana PHP_EOL

Var 2
$1 PHP_EOL
$2 PHP_EOL
$5 PHP_EOL
$6 PHP_EOL

我想加入它们,所以Var 1第一行的结尾与Var2的第一行结合

结果:

apple;orange;banana;$1 PHP_EOL
pear;orange;banana;$2 PHP_EOL
peach;orange;banana;$5 PHP_EOL
melon;orange;banana;$6 PHP_EOL

我试过这个,但它似乎不起作用:

$var1 = explode(PHP_EOL,$fruit);
$var2 = explode(PHP_EOL,$price);

foreach($hist_data1 as $var_h && $change_data1 as $var_c)
{

  $v1 = $var1['0'];
  $v2 = $var2['0'];

  $new_file = $new_file . $v1 . ';' . $v2;
} 

echo $new_file;

但我没有得到我想要的结果。

1 个答案:

答案 0 :(得分:0)

试试这个

<?php
$fruit = 
'apple;orange;banana
pear;orange;banana
peach;orange;banana
melon;orange;banana';

$price = '$1
$2
$5
$6';

$var1 = explode("\r\n",$fruit);
$var2 = explode("\r\n",$price);
print_r($var1);
print_r($var2);
foreach($var1 as $key=>$val)
{
      echo $val.";".$var2[$key].PHP_EOL;
}
?>

输出:

apple;orange;banana;$1
pear;orange;banana;$2
peach;orange;banana;$5
melon;orange;banana;$6

<强> DEMO