Page 1 of 1

Foreach loops without echo or print

Posted: Sat Sep 23, 2006 3:07 am
by blacksnday
How can you get a foreach to output all possible results
without using an echo or print or print_r
and instead by using return

example:

Code: Select all

function id_helper($id_selector)
{
   foreach ($id_selector as $key[]=> $value)
     {
         return $value[$this->idtype()];
     }
}

echo id_helper($_GET['article_id']);
If viewing articles on a page where more then one show
the above function would only show the last(or first) ID number available

I want all possible article id numbers to be displayed intead of just the last(or first) one
Using echo/print is not an option, otherwise this wouldn't be a problem for me.

p.s.
the function probably will not make sense to people,
as I use this method within a class to be used in other classes
to quickly grab article id numbers during runtime....
however, the concept and question for foreach's to spit out all available
output without using echo is still the same ;x

Posted: Sat Sep 23, 2006 4:25 am
by jamiel
Not possible, if you are traversing an array, your output function (echo) needs to be within the loop. Your only other choice is array_walk

Posted: Sat Sep 23, 2006 4:58 am
by aaronhall
Are you only trying to store the function's output in a variable?

Code: Select all

$thisID = id_helper($_GET['article_id']);
We may be able to help you organize the code a little better if you give us some perspective on what you're ultimately trying to accomplish.