473,486 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reading commaseparated string into array

Hi, I need some help on reading commaseparated strings into arrays as numbers.

When I use

$data = "12.4,11.3,13.2";
$myarray = explode(',',$data);

I get strings, not numbers:.

$myarray:array =
0 = "12.4"
1 = "11.3"
2 = "13.2"

where I need

$myarray:array =
0 = 12.4
1 = 11.3
2 = 13.2

What to do?
Jul 17 '05 #1
5 1522
On Mon, 18 Apr 2005 10:58:54 -0400, Martin < > wrote:
$data = "12.4,11.3,13.2";
$myarray = explode(',',$data);
I get strings, not numbers:.


It's probably good enough; whenever you use variables in a numerical
context, they are treated as numbers, even if they are numbers in a
string. If you *have* to make them numbers first, try:

function makefloat( $a )
{
return (float)$a;
}
$numdata = array_map( 'makefloat', $strdata ); // PHP >= 4.0.6
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #2
Ewoud Dronkert <fi*******@lastname.net.invalid> wrote in
news:t3********************************@4ax.com:
On Mon, 18 Apr 2005 10:58:54 -0400, Martin < > wrote:
$data = "12.4,11.3,13.2";
$myarray = explode(',',$data);
I get strings, not numbers:.


It's probably good enough; whenever you use variables in a numerical
context, they are treated as numbers, even if they are numbers in a
string.


You know, I think this *used* to be true, but somewhere mid-PHP4 upgrades,
a lot of my scripts (actually, ALL 'numbers' that were coming via explode
()) stopped working and I had to specifically float() the array contents.

Jul 17 '05 #3
On Mon, 18 Apr 2005 17:26:11 +0200, Ewoud Dronkert <fi*******@lastname.net.invalid> wrote:
On Mon, 18 Apr 2005 10:58:54 -0400, Martin < > wrote:
$data = "12.4,11.3,13.2";
$myarray = explode(',',$data);
I get strings, not numbers:.
It's probably good enough; whenever you use variables in a numerical
context, they are treated as numbers, even if they are numbers in a
string. If you *have* to make them numbers first, try:


Some of the classes I use will not work with arrays of strings, so thanks.
function makefloat( $a )
{
return (float)$a;
}
$numdata = array_map( 'makefloat', $strdata ); // PHP >= 4.0.6


Jul 17 '05 #4
On Mon, 18 Apr 2005 10:57:59 -0500, Good Man wrote:
whenever you use variables in a numerical context, they are treated as
numbers, even if they are numbers in a string.


You know, I think this *used* to be true, but somewhere mid-PHP4 upgrades,
a lot of my scripts (actually, ALL 'numbers' that were coming via explode
()) stopped working and I had to specifically float() the array contents.


Really? This works fine in php 4.3.9 and php5:

foreach ( explode( ',', '1,2.7,-3.14' ) as $n ) echo ($n + 1)."\n";

gives

2
3.7
-2.14
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #5
Ewoud Dronkert <fi*******@lastname.net.invalid> wrote in
news:cr********************************@4ax.com:

You know, I think this *used* to be true, but somewhere mid-PHP4
upgrades, a lot of my scripts (actually, ALL 'numbers' that were
coming via explode ()) stopped working and I had to specifically
float() the array contents.


Really? This works fine in php 4.3.9 and php5:

foreach ( explode( ',', '1,2.7,-3.14' ) as $n ) echo ($n + 1)."\n";

gives

2
3.7
-2.14


interesting.... but yeah, really, scripts that were working fine for over a
year just stopped working correctly (php upgrade) and i had to recode a few
pages and float() each exploded number before multiplying against another
variable (also a number).... i was confused as could be, i guess i still
am!
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1367
by: Row | last post by:
HI, I would first like to say its been about 3 years since looking at java im very rusty! I have to write a post it notes type applet which will function online. (reading from a flat text file)...
6
6570
by: Dietrich Epp | last post by:
Are there any good modules for reading a bitstream? Specifically, I have a string and I want to be able to get the next N bits as an integer. Right now I'm using struct.unpack and bit operations,...
3
9492
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
21
13022
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the...
18
9016
by: John | last post by:
Hi, I'm a beginner is using C# and .net. I have big legacy files that stores various values (ints, bytes, strings) and want to read them into a C# programme so that I can store them in a...
10
8329
by: Tyler | last post by:
Hello All: After trying to find an open source alternative to Matlab (or IDL), I am currently getting acquainted with Python and, in particular SciPy, NumPy, and Matplotlib. While I await the...
0
2172
by: Anish G | last post by:
Hi, I have an issue with reading CSV files. I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads...
21
3010
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
1
4695
by: stoogots2 | last post by:
I have written a Windows App in C# that needs to read a text file over the network, starting from the end of the file and reading backwards toward the beginning (looking for the last occurrence of a...
0
6964
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...
0
7123
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,...
0
7175
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
7319
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...
0
5430
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,...
1
4864
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...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.