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 2 of 2
  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,466
    Thanks
    15
    Thanked 0 Times in 0 Posts

    split string and loop through it

    I have the following code in asp that i'm trying to duplicate in php

    what is does is split a string my commas and tehn i want to loop through it.


    arradminproductfieldnames=split(adminproductfieldnames,",")

    for i= lbound(arradminproductfieldnames) to ubound(arradminproductfieldnames)

    ' do sutff with the element of array
    loop

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    PHP and ASP are similar in how they look and feel, especially with the new object model. The solution you are looking for is like so:

    PHP Code:
    $arradminproductfieldnames explode(", "adminproductfieldnames)

    for (
    $i 0$i count($arradminproductfieldnames); $i++)
    {
        
    // iterate using $arradminproductfieldnames[$i]
    }

    OR
    if (
    is_array($arradminproductfieldnames))
    {
        foreach (
    $arradminproductfieldnames AS $arradminItem
        
    {
            
    // Use $arradminItem as iterated value
        
    }

    Php needs to make the additional check before running a foreach loop to ensure it is an array. This is because a foreach iterates more like a do...while and will throw a warning if it cannot make it through the first step. The basic 'for' loop more closely resembles the loop you have posted.
    Hope that helps you!

    [edit]
    Oh, drop a quick message here as well.
    Php also has a split() function, and will do the same as you are looking for. However, split in php is used in conjunction with regular expressions, and searches for a pattern. This technically makes it slower than an explode for simplistic search techniques.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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