473,801 Members | 2,299 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to turn a string into an array

97 New Member
Hi,

I would like to turn a hidden field value which I receive from an HTML page into an array. It would just be that something like:

[PHP]$layer = "boundaries "[/PHP]

should become:

[PHP]$layer[0] = "boundaries "[/PHP]

But if I use this:

[PHP] if (!is_array($lay er))
{
$layer[0] = $layer;
}[/PHP]

it seems that PHP doesn't consider that $layer has been turned in array. Using the (!is_array) verification just after this, shows that $layer is still not considered as array...

What can/should I do? Thanks for any hints!
Jul 7 '08 #1
3 1955
coolsti
310 Contributor
Expand|Select|Wrap|Line Numbers
  1. if (!is_array($layer))
  2. {
  3.     $tmp = $layer;
  4.     $layer = array();
  5.     $layer[0] = $tmp;
  6. }
  7.  
I would do it this way. I don't know what PHP would do if you tried to redeclare $layer as an array and at the same time initialize its first and only member to the value stored as $layer. So better to break it up and use a variable like $tmp to store the original $layer value.
Jul 7 '08 #2
luftikus143
97 New Member
Expand|Select|Wrap|Line Numbers
  1. if (!is_array($layer))
  2. {
  3.     $tmp = $layer;
  4.     $layer = array();
  5.     $layer[0] = $tmp;
  6. }
  7.  
Great. Thanks a lot!
Jul 7 '08 #3
pbmods
5,821 Recognized Expert Expert
You can also do this:

Expand|Select|Wrap|Line Numbers
  1. $layer = array($layer);
  2.  
Since $layer is a string, when you say $layer[0], you are referencing the first character of $layer rather than converting it to an array.

This will also work:
Expand|Select|Wrap|Line Numbers
  1. http://www.example.com/path/to/script.php?layer[]=boundaries
  2.  
Jul 7 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
4093
by: Nik Coughin | last post by:
Today is the first time I've used MySQL with PHP. What is the best way to turn a column into an array? This is how I am doing it at the moment, surely this is sub-optimal: $query = 'SELECT something FROM table'; $result = mysql_query( $query );
1
4184
by: agascoig | last post by:
I am just starting to learn Java J2RE 5.0. I coded the matrix library below and found that the array accessing is really slow since each access is checked: http://f1.pg.briefcase.yahoo.com/bc/agascoig/vwp2?.tok=bccV1CVBYRRfg_eF&.dir=/Guest&.dnm=ComplexDoubleMatrixJava.zip&.src=bc It appears that with gcj and a -f switch I can turn off the array bounds checking. Will a JVM ever be smart enough to know that if the array bound is a
2
1552
by: George Hester | last post by:
I have this in a style sheet: div.track { font-family: Verdana, Arial, Helvetica, sans-serif; } In a JavaScript function I can pull out Verdana, Arial, Helvetica, sans-serif putting it into a JavaScript variable fontfamily. But what I would prefer is an array fontfamily where each font-family is an element of the array.
5
28058
by: Johnny Meredith | last post by:
Is there a function in VBA to convert a string to an array of characters. I'm looking for the exact same functionality as the ToCharArray() method in C#. If this functionality does not exist, do y'all have any pre-written code to do this? Psuedo-Hopeless Request- Dear Microsoft: It would be cool if VBA had more robust string manipulation functions like those in C#, which kick a*s. TIA
16
17511
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char * is 4 bytes, should the following yield 4, 5, of something else? (And, if something else, what determines the result?) char x = "abcd"; printf( "%d\n", sizeof( x ) ); -Don
7
4367
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
4
8829
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where a * occurs in the string). This split function should allocate a 2D array of chars and put the split results in different rows. The listing below shows how I started to work on this. To keep the program simple and help focus the program the...
2
5432
by: Zarwadi | last post by:
Hi could anyone help me i need to turn this string into a array "070033,070031,070032" dose anyone know how to do this would love the help. Thanks
2
1272
by: javamama | last post by:
Hi, I programmed a web page where two parallel maps can be explored with moving the cursor on the summer and winter buttons. The active button should turn red but now the right hand buttons turn red instead of the left ones. Where's the problem? <HTML> <META HTTP-EQUIV="Refresh" CONTENT=300> <HEAD><TITLE>Parallel Visual Exploring</TITLE></HEAD>
0
9698
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
9556
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
10516
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
10052
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
9101
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
6829
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5616
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2959
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.