Question

Here is my code:

$array=array("x1","x2");
$array=implode(" AND ",$array);
echo $array;

printing out:

x1 AND x2

but i want it automatically print for each values of array to:

x1=x1+y AND x2=x2+y

Was it helpful?

Solution

I'm going out on a limb and assuming that it's not just an array of 2 items, and you want to scale this up. This should work for two items (and more) though.

$returnStr = ""; 
$i = 0;
while ($i < count($array)){
    $returnStr .= $array[$i] . "=" . $array[$i] . "+y" . (($i+1 < count($array))?" AND ":"");
    $i++;
}
echo $returnStr;

OTHER TIPS

<?php
$array=array("x1","x2");
$array[0]=$array[0].'='.$array[0].'+y';
$array[1]=$array[1].'='.$array[1].'+y';
$array=implode(" AND ",$array);
echo $array;

Outputs:
x1=x1+y AND x2=x2+y

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top