Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #8949 > unrolled thread

Need help with stripping characters from numbers in array

Started bydaveh@allheller.net
First post2013-04-05 09:01 -0700
Last post2013-04-05 13:49 -0700
Articles 4 — 3 participants

Back to article view | Back to comp.lang.php


Contents

  Need help with stripping characters from numbers in array daveh@allheller.net - 2013-04-05 09:01 -0700
    Re: Need help with stripping characters from numbers in array Salvatore <sal@yojimbo.hack.invalid> - 2013-04-05 17:43 +0000
    Re: Need help with stripping characters from numbers in array Christoph Becker <cmbecker69@gmx.de> - 2013-04-05 20:41 +0200
    Re: Need help with stripping characters from numbers in array daveh@allheller.net - 2013-04-05 13:49 -0700

#8949 — Need help with stripping characters from numbers in array

Fromdaveh@allheller.net
Date2013-04-05 09:01 -0700
SubjectNeed help with stripping characters from numbers in array
Message-ID<635033e6-79ad-497c-b084-c0ebab234ee8@googlegroups.com>
Hello

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.

I can strip the numbers with this function:

$number = preg_replace("/[[:^digit:]]/", '', $number);

So basically I have to traverse an array element by element check if it is a number and if so strip it otherwise leave it alone and go to next and save the results into an array and the return that array. Or I can simply remove plus, minus or dot chars from every element. (that is all that I would need as that would cover every conceivable scenario)

Can someone suggest how to code this?

Thanks

[toc] | [next] | [standalone]


#8950

FromSalvatore <sal@yojimbo.hack.invalid>
Date2013-04-05 17:43 +0000
Message-ID<kjn2g3$1qh8$1@yojimbo.hack>
In reply to#8949
On 2013-04-05, daveh@allheller.net <daveh@allheller.net> wrote:
> So basically I have to traverse an array element by element check if it is a number and if so strip it otherwise leave it alone and go to next and save the results into an array and the return that array. Or I can simply remove plus, minus or dot chars from every element. (that is all that I would need as that would cover every conceivable scenario)
>
> Can someone suggest how to code this?

A foreach (key => value) loop?

-- 
Blah blah bleh...
GCS/CM d(-)@>-- s+:- !a C++$ UBL++++$ L+$ W+++$ w M++ Y++ b++

[toc] | [prev] | [next] | [standalone]


#8951

FromChristoph Becker <cmbecker69@gmx.de>
Date2013-04-05 20:41 +0200
Message-ID<kjn5rs$rj9$1@speranza.aioe.org>
In reply to#8949
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

[toc] | [prev] | [next] | [standalone]


#8953

Fromdaveh@allheller.net
Date2013-04-05 13:49 -0700
Message-ID<6b59573c-3bff-47ac-99b7-6788b1022ba9@googlegroups.com>
In reply to#8949
Thanks for all the help ... I'm on the right track now.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web