Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    foreach on string

    I would like to do a foreach on this string:

    $mystring="item1, item9, item7, item3, item2, item6, item8";

    I don't know how to do this though. Hope someone can help me

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,247
    Thanks
    12
    Thanked 340 Times in 336 Posts
    if you want to get each "item", explode() by ", ".
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    6
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by manhood View Post
    I would like to do a foreach on this string:

    $mystring="item1, item9, item7, item3, item2, item6, item8";

    I don't know how to do this though. Hope someone can help me
    PHP Code:
    $mystring="item1, item9, item7, item3, item2, item6, item8";
    $myArrayexplode(", "$mystring);
    foreach(
    $myArray as $key => $value){
        echo 
    "item: $value<br />";

    Last edited by johnduarte; 06-22-2010 at 07:15 PM.

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    I'd use single quotes as they're faster, but meh :-P

    Just as an explanation: foreach is used to cycle through an array. Using explode, you can separate a string into an array based on whatever element you're exploding on (', ' in this case). You can then feed that array through foreach. You can also use the shorthand foreach in this case, as the key's don't matter:

    PHP Code:
    foreach ($myArray as $value) {
    //whatever


  • #5
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    6
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Keleth View Post
    I'd use single quotes as they're faster, but meh :-P

    Just as an explanation: foreach is used to cycle through an array. Using explode, you can separate a string into an array based on whatever element you're exploding on (', ' in this case). You can then feed that array through foreach. You can also use the shorthand foreach in this case, as the key's don't matter:

    PHP Code:
    foreach ($myArray as $value) {
    //whatever

    Yes, "Best Practice" is to use single quotes when you can, but it isn't noticeably faster till you hit millions of iterations, and even then it's only fractions of a second faster. So "best practice" but not something you should worry about and go back and change old code to fix.

  • #6
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by johnduarte View Post
    Yes, "Best Practice" is to use single quotes when you can, but it isn't noticeably faster till you hit millions of iterations, and even then it's only fractions of a second faster. So "best practice" but not something you should worry about and go back and change old code to fix.
    Hehe, its why I put the silly smilie after. I didn't mean to hit a chord, was just joking around.

  • #7
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    6
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Keleth View Post
    Hehe, its why I put the silly smilie after. I didn't mean to hit a chord, was just joking around.
    no chord hit, was just clarifying so someone reading it didn't think they should go back and fix all previous code hoping to show vast improvement in speed.

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by johnduarte View Post
    no chord hit, was just clarifying so someone reading it didn't think they should go back and fix all previous code hoping to show vast improvement in speed.
    Oh good god no, yah, its fractions of a second if even. Its a lot about personal taste and purpose too (I don't like having to escape double quotes )
    Last edited by Keleth; 06-22-2010 at 08:12 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •