473,394 Members | 1,829 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,394 software developers and data experts.

problems changing point for coma...

I have an array with numbers from a excel file in 12345,78 format and y
need to change the coma (,) for a point (.) to make this numbers float.
I have this:

str_replace(",",".",$array_with_numbers); but does not work.

Any help???

Mar 7 '06 #1
5 1806
Roco3D wrote:
I have an array with numbers from a excel file in 12345,78 format and y
need to change the coma (,) for a point (.) to make this numbers float.
I have this:

str_replace(",",".",$array_with_numbers); but does not work.

Any help???


A brute force method:
for( $i = 0; $i < count($array_with_numbers); $i++) {
str_replace(',', '.', $array_with_numbers[$i];
}

-david-

Mar 7 '06 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

David Haynes wrote:
A brute force method:
for( $i = 0; $i < count($array_with_numbers); $i++) {
str_replace(',', '.', $array_with_numbers[$i];
}


Never use a for(;;) loop to iterate over an array: you are creating
error-prone code.

Either use foreach() with references:
<?php
foreach($array_with_numbers as &number)
str_replace(',', '.', $number);
?>

Or use array_map to automatically transverse the array applying a function
to every element, or (even better) use a recursive function.

Or, you can RTFM. http://php.net/str_replace , the part that says:

"If subject is an array, then the search and replace is performed with every
entry of subject, and the return value is an array as well."
So, please go RTFM.

- --
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

"All truth passes through three stages. First, it is ridiculed. Second, it
is violently opposed. Third, it is accepted as being self-evident."
- Arthur Schopenhauer (1788-1860)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFEDf863jcQ2mg3Pc8RAn5MAJ9eJ+FYy7Zh6Dmr5O1Ufk IIGdnLTwCbBmKZ
VD+NBSUgRGFj9HBbG206CSI=
=VWqE
-----END PGP SIGNATURE-----
Mar 7 '06 #3
Iván Sánchez Ortega wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

David Haynes wrote:
A brute force method:
for( $i = 0; $i < count($array_with_numbers); $i++) {
str_replace(',', '.', $array_with_numbers[$i];
}


Never use a for(;;) loop to iterate over an array: you are creating
error-prone code.

Either use foreach() with references:
<?php
foreach($array_with_numbers as &number)
str_replace(',', '.', $number);
?>

Or use array_map to automatically transverse the array applying a function
to every element, or (even better) use a recursive function.

Or, you can RTFM. http://php.net/str_replace , the part that says:

"If subject is an array, then the search and replace is performed with every
entry of subject, and the return value is an array as well."
So, please go RTFM.


I suspect you are having a bad day.
Iteration by indexing works and is, as I said, brute force.

-david-

Mar 7 '06 #4
David Haynes wrote:
Iteration by indexing works and is, as I said, brute force.


Iteration by indexing works *iff* all your indexes are numeric and
sequential. Your for-loop won't work on non-sequential arrays like this:

array(
0 => '123,45',
1 => '123,45',
9 => '123,45'
);

because count() will return 3, meaning that the last element of the array,
with index 9, won't be touched. Nor will it work on:

array(
-1 => '123,45',
0 => '123,45',
1 => '123,45'
);

as your for-loop has a hard-coded start value of 0. Nor will it work on:

array(
'a' => '123,45',
'b' => '123,45',
'c' => '123,45'
);

because the keys aren't numeric. Iván's foreach-loop will work on all of
these three examples.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Mar 8 '06 #5
Toby Inkster wrote:
David Haynes wrote:
Iteration by indexing works and is, as I said, brute force.


Iteration by indexing works *iff* all your indexes are numeric and
sequential. Your for-loop won't work on non-sequential arrays like this:

array(
0 => '123,45',
1 => '123,45',
9 => '123,45'
);

because count() will return 3, meaning that the last element of the array,
with index 9, won't be touched. Nor will it work on:

array(
-1 => '123,45',
0 => '123,45',
1 => '123,45'
);

as your for-loop has a hard-coded start value of 0. Nor will it work on:

array(
'a' => '123,45',
'b' => '123,45',
'c' => '123,45'
);

because the keys aren't numeric. Iván's foreach-loop will work on all of
these three examples.

Tony,
Thank you for a well presented response. I guess I tend to treat
associative and numeric arrays differently (in my own head) and so would
not even have thought of using a for() on an associative array, but the
difficulties with non-zero origin and sparse data are real.

Lesson learned.
-david-

Mar 8 '06 #6

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

Similar topics

6
by: Ed | last post by:
Here's the scene: 1) XP Pro, with IIS installed and running, as well as complete .NET Architect Enterprise development environment 2) Tomcat server 4.1.27 in the default directory...
1
by: Rolan | last post by:
Having tried various permutations of Before Update and well for that matter, After Update, OnExit, OnEnter, etc. and also Locked controls, I'm still unable to obtain the intended results. There are...
1
by: Rod | last post by:
We have a .NET application which is installed on one machine (let's call it server 2), and we point to it using IIS from another machine (our edge server, which I'll call server 1). So, server 1...
2
by: Jim Frazer | last post by:
Hi, I'm working on an application in C# that will allow the user to create simple CAD drawings on a CEPC system. I would like to be able to change the cursor shape depending on the drawing mode...
17
by: Frederick Gotham | last post by:
I know there's a sequence point at a comma, e.g.: int main(void) { int a = 1; a++, ++a, a *= 3, a <<= 4; /* Perfectly okay */ }
13
by: Massimo Fabbri | last post by:
Maybe it's a little OT, but I'll give it try anyway.... I was asked to maintain and further develop an already existing small company's web site. I know the golden rule of "eternal" URIs, but...
9
by: =?Utf-8?B?SG93YXJkIFNtaXRo?= | last post by:
I am using VC++ 6.0 (with SP5 installed). When using WinXP this is with SP2 installed. I am developing an instrumentation system comprising a set of networked PCs connected using TCP/IP TCP links....
0
by: drawing in aspnet | last post by:
Question about putting the data layer in a separate class library. I keep reading that the data layer should be separated from the presentation layer and put in its own class library. I am...
17
by: spooler123 | last post by:
Just a small little program. Can not figure out what am I doing wrong. #include <stdio.h> #include <limits.h> #include <float.h> int main() { double max = FLT_MAX;
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?
0
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.