I have this: <?php $titles[3] = 'three'; $titles[2] = 'two'; $titles[1] = 'one'; foreach ($titles as $t ) { print "title=$t "; } ?> Code (markup): I don't want to print, I need to add all those values into a database, so I need all the values in a list, as a single variable. Something like $x="one, two, three"; If I change the "print" with a "return" only the last array gets listed.. Amy idea how to do what I need?
<?php $titles[3] = 'three'; $titles[2] = 'two'; $titles[1] = 'one'; foreach ($titles as $t ) { $result .= $titles[$t]; } echo($result); //To test to see if it is properly displayed. ?> PHP: That should work. If not, let me know.
You need to use Key Value method in your for each loop to provide a new index to the titles array everytime to get the next value. $result is declared before the loop so it can be used outside the loop as well. Please let us know if it works, works here with me.
Try this $titles[3] = 'three'; $titles[2] = 'two'; $titles[1] = 'one'; $titles = array_reverse($titles, true); $s = implode(", ", $titles); echo $s; Code (markup):