Foreach loops without echo or print

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Foreach loops without echo or print

Post 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
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post 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
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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.
Post Reply