By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,442 Members | 1,580 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,442 IT Pros & Developers. It's quick & easy.

Know any slick ways to build a string from array?

P: 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
Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
D. Alvarado <la***********@zipmail.com> wrote:
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.


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

Jul 17 '05 #2

P: n/a
Daniel Tryba <ne****************@canopus.nl> wrote:
"<option value=\"1\">January</option>\n<option
value=\"2\">February</option>\n<option value=\"3\">March</option> ...
December</option>"
But more readable might be: http://php.net/foreach (second code)


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

Daniel Tryba

Jul 17 '05 #3

P: n/a
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";
}
Jul 17 '05 #4

P: n/a
la***********@zipmail.com (D. Alvarado) wrote in message news:<9f**************************@posting.google. com>...
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


$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?
Jul 17 '05 #5

P: n/a
I noticed that Message-ID:
<29*************************@posting.google.com> from harryman100
contained the following:
$string = "";
foreach ($months AS $key => $month) {
$string .= "<option value=\"$key\">$month</option>\n";
}


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/
Jul 17 '05 #6

P: n/a
On Fri, 24 Sep 2004 13:39:53 +0100, Geoff Berrow
<bl******@ckdog.co.uk> wrote:
I noticed that Message-ID:
<29*************************@posting.google.com > from harryman100
contained the following:
$string = "";
foreach ($months AS $key => $month) {
$string .= "<option value=\"$key\">$month</option>\n";
}


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>";


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
================================================== =========================
Jul 17 '05 #7

P: n/a
I noticed that Message-ID: <cj**********@blackhelicopter.databasix.com>
from Gary L. Burnore contained the following:
Is one str_replace faster than 12 if's?


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/
Jul 17 '05 #8

P: n/a
I noticed that Message-ID: <cj**********@blackhelicopter.databasix.com>
from Gary L. Burnore contained the following:
Is one str_replace faster than 12 if's?


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/
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.