Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.