循环三个PHP数组

时间:2015-11-25 20:09:08

标签: arrays loops

我有三个PHP数组,我想在同一个循环中使用它们来获取值。

$advertising_advpic   = "photo src1,photo src2,photo src3";
$advertising_advlink  = "site link1,site link2,site link3";
$advertising_advtitle = "site name1,site name2,site name3";

$advpic   = explode(",",$advertising_advpic);
$advlink  = explode(",",$advertising_advlink);
$advtitle = explode(",",$advertising_advtitle);


for ( $i = 0; $i<count($advpic); $i++)
{
    $link  = $advlink [$i];
    $pic   = $advpic  [$i];
    $title = $advtitle[$i];
    $all   = "<td nowrap><a target='_blank' href='".$link."'><img src='".$pic."' border='0' alt='".$title."'></a></td>";
}

但是当我打印$all时,我只得到最后一个值。

1 个答案:

答案 0 :(得分:2)

试试这个

$advertising_advpic   = "photo src1,photo src2,photo src3";
$advertising_advlink  = "site link1,site link2,site link3";
$advertising_advtitle = "site name1,site name2,site name3";

$advpic = explode(",",$advertising_advpic);
$advlink = explode(",",$advertising_advlink);
$advtitle = explode(",",$advertising_advtitle);
$all = "";

for ( $i = 0; $i<count($advpic); $i++)
{
    $link   = $advlink[$i];
    $pic = $advpic[$i];
    $title  = $advtitle[$i];
    $all = $all . "<td nowrap><a target='_blank' href='".$link."'><img src='".$pic."' border='0' alt='".$title."'></a></td>";
}

你在循环的每一次迭代中不断重新获得$之前的方式。