473,698 Members | 2,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Know any slick ways to build a string from array?

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\">Jan uary</option>\n<optio n
value=\"2\">Feb ruary</option>\n<optio n value=\"3\">Mar ch</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
8 2189
D. Alvarado <la***********@ zipmail.com> wrote:
and I would like to take this array and build a string of the form

"<option value=\"1\">Jan uary</option>\n<optio n
value=\"2\">Feb ruary</option>\n<optio n value=\"3\">Mar ch</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
Daniel Tryba <ne************ ****@canopus.nl > wrote:
"<option value=\"1\">Jan uary</option>\n<optio n
value=\"2\">Feb ruary</option>\n<optio n value=\"3\">Mar ch</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
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
la***********@z ipmail.com (D. Alvarado) wrote in message news:<9f******* *************** ****@posting.go ogle.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\">Jan uary</option>\n<optio n
value=\"2\">Feb ruary</option>\n<optio n value=\"3\">Mar ch</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
I noticed that Message-ID:
<29************ *************@p osting.google.c om> 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){$s elected="select ed";} else{$selected= "";}
$string .= "<option value=\"$key\" $selected>$mont h</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
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){$s elected="select ed";} else{$selected= "";}
$string .= "<option value=\"$key\" $selected>$mont h</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>$mont h</option>\n";
}

$string.="</select>";

if(isset($_POST['month']) {
$k=$_POST['month'];
$string = str_replace("\$ k\">","\"$k\" selected>", $string);
}
--
gburnore@databa six 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
I noticed that Message-ID: <cj**********@b lackhelicopter. 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
I noticed that Message-ID: <cj**********@b lackhelicopter. 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2437
by: Phil Powell | last post by:
Consider this: class ActionHandler { /*------------------------------------------------------------------------------------------------------------------------------------------------------------------- This class will be a singleton class structure by calling its constructor by reference only (prefixed by '&'). Is primarly used to reference its errorArray Array property as a single reference to allow for static usage
0
276
by: gunaselvas | last post by:
Can u tell me how to configure the build options in Visual Slick Editor on Project Property tabs
1
1418
by: Phillll Peeps via .NET 247 | last post by:
Hi there, hope you can help How can i put the contents of the clipboard into a two dimensional array of type double? i can get the data with: Dim iData As IDataObject = Clipboard.GetDataObject() i want to build a dynamic array based on the amount of data in the clipboard so if i copied the 2 lines: 1 2 3
12
2441
by: Peter Lin | last post by:
Hey, I am just wondering if anyone has got any idea of setting up a new class so that you could just print like the old ways with the printer class, since I am writing a program that really requires simple black and white text printing, but there is alot to print and it's needed at various places, which is why I find that the new printing style with the printdocument a bit too clumsy. Any help would be appreciated.
3
34883
by: laredotornado | last post by:
Hi, Wondered if there was a good one-liner for what I want to do in PHP 4. I have an array, with an arbitrary number of elements. I'd like to know if all the elements in the array are empty. If at least one element is non-empty, then the entire condition is false. $my_arr = array(); $my_arr = ""; $my_arr = "";
12
112092
by: manochavishal | last post by:
Hi, I have a question. How can i know the size of array when it is passed to a function. For Example i have this code: #include <stdio.h> #include <stdlib.h>
38
2315
by: Zytan | last post by:
What is the difference between these two lines? Dim args As Object() = New Object() {strText} Dim args As Object() = {strText} args seems usuable from either, say, like so: Me.Invoke(delegate, args) Zytan
10
2817
by: Mo | last post by:
Hi, I am trying to write a code to build a string 768 characters long. This string is going to be written to a file which is then read by another application. The format of the string is already defined. For example Firstname starts at position 20, Last Name at position 30, address at position 40 etc... I am reading these data from sql datasource and must substitute the values in the exact position insuring that paramteres end up...
3
1836
by: Hugh Oxford | last post by:
I want to build a string to reference an object. I can reference is manually thus: print_r($this->struct->parts->parts); but if I build a string... $string = "->parts->parts"
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8610
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7740
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.