TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-25-2008, 05:21 PM   #1 (permalink)
The Contributor
 
marxx's Avatar
 
Join Date: Sep 2007
Location: Finland
Posts: 45
Thanks: 3
marxx is on a distinguished road
Default Explode an array + add start and end tags?

Hi!

Here's my problem.

How do I explode (or another technique?) an array so I can add every item start and end tags?

Code:
$items = array('item1', 'item2', 'item3', 'item4');
Each those item should print like this:
Code:
<li>item1</li>
<li>item2</li>
<li>item3</li>
<li>item4</li>
This might be little noobie question, thanks for all help!
Send a message via MSN to marxx
marxx is offline  
Reply With Quote
Old 09-25-2008, 05:33 PM   #2 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default

Something like this should do the trick for you:
php Code:
<?php
    foreach($items as $item) {
        echo '<li>'.$item.'</li>'."\n";
    }
?>

-- or depending on the style you use

php Code:
<?php
    foreach($items as $item) {
        echo "<li>$item</li>\n";
    }
?>

But either should work for you
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll
buggabill is offline  
Reply With Quote
The Following 2 Users Say Thank You to buggabill For This Useful Post:
marxx (09-25-2008), webid (09-25-2008)
Old 09-25-2008, 05:49 PM   #3 (permalink)
The Contributor
 
marxx's Avatar
 
Join Date: Sep 2007
Location: Finland
Posts: 45
Thanks: 3
marxx is on a distinguished road
Default

Wow - That was fast!!

Thanks for code, that does the job! It's so clear now how to do it... ;)
Send a message via MSN to marxx
marxx is offline  
Reply With Quote
Old 09-25-2008, 05:55 PM   #4 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default

Right place - right time I suppose...

No problem!
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll
buggabill is offline  
Reply With Quote
Old 09-26-2008, 02:21 AM   #5 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
RobertK is on a distinguished road
Default

Or you could do what I did:
PHP Code:
echo '<li>'.implode('</li><li>'$items).'</li>'
It's all a stylistic preference. I don't actually know if this is any faster.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
Old 09-26-2008, 11:24 AM   #6 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

or:
PHP Code:
array_walk($itemscreate_function('$s''echo \'<li>\'.$s.\'</li>\';')); 
although i wouldn't advise it personally.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 09-26-2008, 11:36 AM   #7 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

With the newer versions of PHP (5.3 I think) it's also possible to do:
PHP Code:
$items = array('item1''item2''item3''item4');

array_walk($items, function($item){
    echo 
'<li>'$item'</li>'PHP_EOL;
}); 
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
RobertK (09-26-2008)
Old 09-26-2008, 12:02 PM   #8 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Yep proper lambda functions, cant wait for PHP5.3 although why on earth would they want to add a GOTO statement?.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:53 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design