Jump to content

Number Series (1,2,3,4,5)


RomanRemake

Recommended Posts

I would suggest using a for() loop, or merge()+range()... but if you HAVE to use a foreach loop...

 

<?php
$array = array('Dogs', 'Cats', 'Squirrels', 'Mice', 'Rats');
$i = 1;
$newArray = array();

foreach($array as $element)
{
  $newArray[] = "$i. $element";
  ++$i;
}
?>

 

If you're using it to display in html, I would suggest the method above.

Link to comment
https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626514
Share on other sites

If these are in an array, you can do this:

<?php
$things = array('Dogs','Cats','Squirrels','Mice','Rats');
echo '<ol><li>' . implode('</li><li>',$things) . '</li></ol>';
?>

 

Ken

 

Ooh, that is slick!

 

Yeh :P I just tested it and it worked, but I dont get how imploding an array works :S

Link to comment
https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626530
Share on other sites

You'll have to put the items into an array, but you don't have to use foreach()... You could ues a simple for() like so:

 

<?php

$info[1] = "Dogs";
$info[2] = "Cats";
$info[3] = "Squirrels";
$info[4] = "Mice";
$info[5] = "Rats";

for($i = 1; $i <= count($info); $i++){
   echo "$i. $info[$i]"."<br>";
}

?>

 

Outputs:

 

1. Dogs

2. Cats

3. Squirrels

4. Mice

5. Rats

Link to comment
https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626532
Share on other sites

If these are in an array, you can do this:

<?php
$things = array('Dogs','Cats','Squirrels','Mice','Rats');
echo '<ol><li>' . implode('</li><li>',$things) . '</li></ol>';
?>

 

Ken

 

Ooh, that is slick!

 

Yeh :P I just tested it and it worked, but I dont get how imploding an array works :S

 

an array is all you can implode http://us.php.net/implode

Link to comment
https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626533
Share on other sites

Thank you for all of the replies (and so fast!).

 

Sorry for not clarifying. I'm pulling the info from a database. Here's the current code:

 

  <?php foreach($listings AS $listing):?>

  <?php echo $Routes->content($listing['Listing']['title'],$listing);?>

    <?php endforeach;?>

 

Take care,

RR

 

 

Link to comment
https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626544
Share on other sites

for the record, we should be in the habit of running the count() function outside of the for() loop, otherwise the loop runs it on every iteration and wastes valuable DIGIRESOURCES!!

 

NOT THE DIGIRESOURCES!!!111oneleven

 

:s my bad, long time since I used implode/explode lol

 

but they're EXPLODE and IMPLODE... they're the coolest named functions in the language..

 

Of course they lose to fopen (phhhhopen!!!!) in the "most fun to say" category.

 

Yeah, I hold my own language awards where I give awards to the "most *" functions in the library... that doesn't make me weird, right?

 

.. I'll shut up now.

Link to comment
https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626611
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.