473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

How to turn a string into an array

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($layer))
{
$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 1942
coolsti
310 100+
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
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 Expert 4TB
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
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...
1
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: ...
2
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...
5
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...
16
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 *...
7
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...
4
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...
2
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.