sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
D. Alvarado's Avatar

Know any slick ways to build a string from array?


Question posted by: D. Alvarado (Guest) on July 17th, 2005 09:45 AM
Hi, I have this arr

$months = array("January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November",
"December");

and I would like to take this array and build a string of the form

"<option value=\"1\">January</option>\n<option
value=\"2\">February</option>\n<option value=\"3\">March</option> ...
December</option>"

Does anyone know a slick way to get to the above string from the given
array? I'm using PHP 4.

Thanks, - Dave
8 Answers Posted
Daniel Tryba's Avatar
Guest - n/a Posts
#2: Re: Know any slick ways to build a string from array?

D. Alvarado <laredotornado@zipmail.com> wrote:[color=blue]
> and I would like to take this array and build a string of the form
>
> "<option value=\"1\">January</option>\n<option
> value=\"2\">February</option>\n<option value=\"3\">March</option> ...
> December</option>"
>
> Does anyone know a slick way to get to the above string from the given
> array? I'm using PHP 4.[/color]

Yes, I know of several. And they are all in the fine manual, which you
should read offcourse.

how about:
$str='prepend'.join('glue', $arr).'append';

But more readable might be: http://php.net/foreach (second code)

--

Daniel Tryba

Daniel Tryba's Avatar
Guest - n/a Posts
#3: Re: Know any slick ways to build a string from array?

Daniel Tryba <news_comp.lang.php@canopus.nl> wrote:[color=blue][color=green]
>> "<option value=\"1\">January</option>\n<option
>> value=\"2\">February</option>\n<option value=\"3\">March</option> ...
>> December</option>"[/color][/color]
[color=blue]
> But more readable might be: http://php.net/foreach (second code)[/color]

Ehhh, forget the join, read the 3rd example of the foreach url.


--

Daniel Tryba

Rasmus Rimestad's Avatar
Rasmus Rimestad July 17th, 2005 09:45 AM
Guest - n/a Posts
#4: Re: Know any slick ways to build a string from array?

Using the standard way usually works best. It might not be extremely
slick, but four lines is not to much. Code has not been tested.

$months = array("January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November",
"December");

$size = sizeof($months);
for($i = 0; $i < $size; $i++) {
$str .= "<option value=\"" . ($i + 1) . "\">" . $months[$i] .
"</option>\n";
}
harryman100's Avatar
Guest - n/a Posts
#5: Re: Know any slick ways to build a string from array?

Join Bytes! (D. Alvarado) wrote in message news:<9fe1f2ad.0409201845.6de0c773@posting.google.com>...[color=blue]
> Hi, I have this arr
>
> $months = array("January", "February", "March", "April", "May",
> "June", "July", "August", "September", "October", "November",
> "December");
>
> and I would like to take this array and build a string of the form
>
> "<option value=\"1\">January</option>\n<option
> value=\"2\">February</option>\n<option value=\"3\">March</option> ...
> December</option>"
>
> Does anyone know a slick way to get to the above string from the given
> array? I'm using PHP 4.
>
> Thanks, - Dave[/color]

$string = "";
foreach ($months AS $key => $month) {
$string .= "<option value=\"$key\">$month</option>\n";
}

$string now contains exactly what you had before. Is that slick enough?
Geoff Berrow's Avatar
Guest - n/a Posts
#6: Re: Know any slick ways to build a string from array?

I noticed that Message-ID:
<29cee28.0409210102.620134bb@posting.google.com> from harryman100
contained the following:
[color=blue]
>$string = "";
>foreach ($months AS $key => $month) {
> $string .= "<option value=\"$key\">$month</option>\n";
>}[/color]

Slick, but I prefer this (untested)..

if(isset($_POST['month']) {$k=$_POST['month'];} else {$k="";}

$string = "<select name="month">";
foreach ($months AS $key => $month) {
if($key==$k){$selected="selected";} else{$selected="";}
$string .= "<option value=\"$key\" $selected>$month</option>\n";
}
$string.="</select>";
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Gary L. Burnore's Avatar
Gary L. Burnore July 17th, 2005 09:49 AM
Guest - n/a Posts
#7: Re: Know any slick ways to build a string from array?

On Fri, 24 Sep 2004 13:39:53 +0100, Geoff Berrow
<blthecat@ckdog.co.uk> wrote:
[color=blue]
>I noticed that Message-ID:
><29cee28.0409210102.620134bb@posting.google.com> from harryman100
>contained the following:
>[color=green]
>>$string = "";
>>foreach ($months AS $key => $month) {
>> $string .= "<option value=\"$key\">$month</option>\n";
>>}[/color]
>
>Slick, but I prefer this (untested)..
>
>
>$string = "<select name="month">";
>foreach ($months AS $key => $month) {
> if($key==$k){$selected="selected";} else{$selected="";}
> $string .= "<option value=\"$key\" $selected>$month</option>\n";
>}
>$string.="</select>";[/color]

Is one str_replace faster than 12 if's? If so, then:


$string = "<select name="month">";

foreach ($months AS $key => $month) {
$string .= "<option value=\"$key\" $selected>$month</option>\n";
}

$string.="</select>";

if(isset($_POST['month']) {
$k=$_POST['month'];
$string = str_replace("\$k\">","\"$k\" selected>", $string);
}


--
gburnore@databasix dot com
---------------------------------------------------------------------------
How you look depends on where you go.
---------------------------------------------------------------------------
Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
| ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
| ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³
Black Helicopter Repair Svcs Division | Official Proof of Purchase
================================================== =========================
Want one? GET one! http://signup.databasix.com
================================================== =========================
Geoff Berrow's Avatar
Guest - n/a Posts
#8: Re: Know any slick ways to build a string from array?

I noticed that Message-ID: <cj15hg$lgo$1@blackhelicopter.databasix.com>
from Gary L. Burnore contained the following:
[color=blue]
>Is one str_replace faster than 12 if's?[/color]

Cool, thanks Gary. :-)

I'm running a basic PHP course now and, as I tell my students, there is
always more than one way to do it . :-)


--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Geoff Berrow's Avatar
Guest - n/a Posts
#9: Re: Know any slick ways to build a string from array?

I noticed that Message-ID: <cj15hg$lgo$1@blackhelicopter.databasix.com>
from Gary L. Burnore contained the following:
[color=blue]
>Is one str_replace faster than 12 if's?[/color]

Actually, thinking about this, how many comparisons does a string
replace have to do?

Anyone know the definitive answer? (to save me running tests,:-)

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
 
Not the answer you were looking for? Post your question . . .
197,045 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,045 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors