Connecting Tech Pros Worldwide Help | Site Map

Know any slick ways to build a string from array?

  #1  
Old July 17th, 2005, 09:45 AM
D. Alvarado
Guest
 
Posts: n/a
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
  #2  
Old July 17th, 2005, 09:45 AM
Daniel Tryba
Guest
 
Posts: n/a

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

  #3  
Old July 17th, 2005, 09:45 AM
Daniel Tryba
Guest
 
Posts: n/a

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

  #4  
Old July 17th, 2005, 09:45 AM
Rasmus Rimestad
Guest
 
Posts: n/a

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";
}
  #5  
Old July 17th, 2005, 09:45 AM
harryman100
Guest
 
Posts: n/a

re: Know any slick ways to build a string from array?


laredotornado@zipmail.com (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?
  #6  
Old July 17th, 2005, 09:49 AM
Geoff Berrow
Guest
 
Posts: n/a

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/
  #7  
Old July 17th, 2005, 09:49 AM
Gary L. Burnore
Guest
 
Posts: n/a

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
================================================== =========================
  #8  
Old July 17th, 2005, 09:50 AM
Geoff Berrow
Guest
 
Posts: n/a

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/
  #9  
Old July 17th, 2005, 09:51 AM
Geoff Berrow
Guest
 
Posts: n/a

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/
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 04:15 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 11:37 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 09:56 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 03:15 AM