Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 7th, 2006, 08:55 PM
Roco3D
Guest
 
Posts: n/a
Default 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???

  #2  
Old March 7th, 2006, 09:15 PM
David Haynes
Guest
 
Posts: n/a
Default 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-

  #3  
Old March 7th, 2006, 10:25 PM
Iván Sánchez Ortega
Guest
 
Posts: n/a
Default 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-----
  #4  
Old March 7th, 2006, 10:35 PM
David Haynes
Guest
 
Posts: n/a
Default 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-

  #5  
Old March 8th, 2006, 09:15 AM
Toby Inkster
Guest
 
Posts: n/a
Default 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

  #6  
Old March 8th, 2006, 11:25 AM
David Haynes
Guest
 
Posts: n/a
Default 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-

 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles