FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Need help with stripping characters from numbers in array
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Need help with stripping characters from numbers in array [message #181011 is a reply to message #181009] Fri, 05 April 2013 18:41 Go to previous messageGo to previous message
Christoph Becker is currently offline  Christoph Becker
Messages: 91
Registered: June 2012
Karma:
Member
daveh wrote:

> Given I have an array as such: (as an example)
>
> Array ([0]=>+1.,[1]=>+2.,[2]=>-3.0,[3]=>A3B)
>
> I want to return an array as such:
> Array ([0]=>1,[1]=>2,[2]=>3,[3]=>A3B)
>
> basically if its a number remove leading + or - char from number and
> any decimal point but do not do anything with letters or anything
> with number and char combination such as 2B or A3 and so forth.

You may consider to slightly rewrite the requirements:

If it's a number, strip superfluous details; otherwise leave it as it is.

This specification leads to the following implementation:

$elt = is_numeric($elt) ? floatval($elt) : $elt;

To return the modified array array_map() seems to be appropriate.

Please wrap your lines to a reasonable width (see e.g.
<http://www.softdevlabs.com/personal/Usenet101.html#LineWrap>). TIA.

--
Christoph M. Becker
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Previous Topic: C,C++ EMBEDDED SYSTEM, DSP CONSULTANT AVAILABLE
Next Topic: comparing arrays
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 13:52:03 GMT 2024

Total time taken to generate the page: 0.05905 seconds