View Full Version : split string and loop through it
03-11-2007, 01:14 PM
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
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:
$arradminproductfieldnames = explode(", ", adminproductfieldnames)
for ($i = 0; $i < count($arradminproductfieldnames); $i++)
// iterate using $arradminproductfieldnames[$i]
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!
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.
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.