Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
Thread: split string and loop through it
03-11-2007, 01:14 PM #1
- Join Date
- May 2004
- 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.
for i= lbound(arradminproductfieldnames) to ubound(arradminproductfieldnames)
' do sutff with the element of array
03-11-2007, 08:21 PM #2
- Join Date
- Sep 2002
- Saskatoon, Saskatchewan
- Thanked 2,660 Times in 2,629 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 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.PHP Code:
$arradminproductfieldnames = explode(", ", adminproductfieldnames)
for ($i = 0; $i < count($arradminproductfieldnames); $i++)
// iterate using $arradminproductfieldnames[$i]
foreach ($arradminproductfieldnames AS $arradminItem
// Use $arradminItem as iterated value
Hope that helps you!
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.
header('HTTP/1.1 420 Enhance Your Calm');