Connecting Tech Pros Worldwide Forums | Help | Site Map

problems changing point for coma...

Roco3D
Guest
 
Posts: n/a
#1: Mar 7 '06
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???


David Haynes
Guest
 
Posts: n/a
#2: Mar 7 '06

re: problems changing point for coma...


Roco3D wrote:[color=blue]
> 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???
>[/color]

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

-david-

Iván Sánchez Ortega
Guest
 
Posts: n/a
#3: Mar 7 '06

re: problems changing point for coma...


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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

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-----
David Haynes
Guest
 
Posts: n/a
#4: Mar 7 '06

re: problems changing point for coma...


Iván Sánchez Ortega wrote:[color=blue]
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> David Haynes wrote:
>[color=green]
>> A brute force method:
>> for( $i = 0; $i < count($array_with_numbers); $i++) {
>> str_replace(',', '.', $array_with_numbers[$i];
>> }[/color]
>
> 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.[/color]

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

-david-

Toby Inkster
Guest
 
Posts: n/a
#5: Mar 8 '06

re: problems changing point for coma...


David Haynes wrote:
[color=blue]
> Iteration by indexing works and is, as I said, brute force.[/color]

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

David Haynes
Guest
 
Posts: n/a
#6: Mar 8 '06

re: problems changing point for coma...


Toby Inkster wrote:[color=blue]
> David Haynes wrote:
>[color=green]
>> Iteration by indexing works and is, as I said, brute force.[/color]
>
> 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.
>[/color]
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-

Closed Thread


Similar PHP bytes