Leon83
Leon83

Reputation: 134

How to replace numbers within a array in php

Can you please help me to search the array for decimal numbers or part of it like ". "(dot and space after it) and replace it with zeros or remove the dot?

Below is part of an array:

Array ( [2] => AUD,ADF,1,06-01-2001,3.,3.9532
        [3] => AUD,ADP,1,06-02-2001,99.8222,99.6682
        [4] => AUD,AED,1,06-01-2001,1.8687,1.8664
        [5] => AUD,AFA,1,06-01-2001,2416.57,2413.95
        [6] => AUD,AFN,1,06-01-2001,2416.57,2413.95
        [7] => AUD,ALL,1,06-01-2001,77.,75.9759
        [8] => AUD,AMD,1,06-03-2001,NULL,NULL
        [9] => AUD,ANG,1,06-01-2001,0.9056,0.9046
        [10] => AUD,AOA,1,06-01-2001,3.0751,2.9961
        [11] =>

Please look at [7] there is number like 77. or at [2] there is 3. (there is nothing after the dot).

Upvotes: 2

Views: 205

Answers (4)

HamZa
HamZa

Reputation: 14921

Using preg_replace():

$array = array(
    'AUD,ADF,1,06-01-2001,3.,3.9532',
    'AUD,ADP,1,06-02-2001,99.8222,99.6682',
    'AUD,ALL,1,06-01-2001,77.,75.9759',
    'AUD,ALL,1,06-01-2001,78.   ,75.9759'
);
//                         ----RegEx Pattern---  Replace   Input
//                         \/       \/       \/    \/       \/
$new_array = preg_replace('/(\d+)\.(\s*)(?!\d)/', '$1', $array);
//                           ^^^ ^^ ^^^^ ^^^^
//                            |   |   |    | 
// Match any digits [0-9] <---|   |   |    |
//         Match a point  <-------|   |    |
// Match spaces and make it optional<--    |
//                                         |
// Lookahead, which means that if there <--|
// is no digits after:                  <--|
// digit[point][0 or several spaces]    <--|
// The whole thing won't be matched.    <--|

// Addition: the first (\d+) is the first match group
// That's why we used $1 in replace !

// \d -> [0-9]
// \s -> space
// \. -> points have to be escaped
// (?!) -> Lookbehind (check : http://php.net/manual/en/regexp.reference.assertions.php)
// * -> Occurs 0 time or plus
// + -> occurs 1 time or plus
// The '/' at the begin and the end are delimiters

print_r($new_array);

Output:

Array
(
    [0] => AUD,ADF,1,06-01-2001,3,3.9532
    [1] => AUD,ADP,1,06-02-2001,99.8222,99.6682
    [2] => AUD,ALL,1,06-01-2001,77,75.9759
    [3] => AUD,ALL,1,06-01-2001,78,75.9759
)

EDIT: To answer the question that's in the comment below:

Regular Expressions is all about regular patterns. From the desired output, I can see that you want to put everything in quotes except numbers (integer and double/float), we also have dates to put between quotes. So here's a way to do it:

$new_array = preg_replace('/(\d+)\.(\s*)(?!\d)/', '$1', $array); // remove some dots & spaces
$new_array_2 = preg_replace('/([a-zA-Z]+|\d+-\d+-\d+)/', '\'$1\'', $new_array);
//            Match letters <--^-^-^-^-^  ^-^-^-^-^---> Match digits-digits-digits (for the date part), I don't want to use a LOOOONG RegEx to check if it's valid ...

print_r($new_array); // First replacement
print_r($new_array_2); // Second replacement

There is another more reliable way to do this, by using some PHP-Fu:

$new_array = preg_replace('/(\d+)\.(\s*)(?!\d)/', '$1', $array); // remove some dots & spaces
// For this code YOU NEED PHP 5.3+ since it's using anonymous functions
$new_array_2 = array_map(function($val){
    $pieces = explode(',', $val);
    $pieces2 = array_map(function($val2){
        if(preg_match('/^\d+(\.\d+)?$/', $val2)){
            return $val2;
        }else{
            return "'$val2'";
        }
    }, $pieces);
    return(implode(',',$pieces2));
}, $new_array);

print_r($new_array);
print_r($new_array_2);

Output:

Array
(
    [0] => AUD,ADF,1,06-01-2001,3,3.9532
    [1] => AUD,ADP,1,06-02-2001,99.8222,99.6682
    [2] => AUD,ALL,1,06-01-2001,77,75.9759
    [3] => AUD,ALL,1,06-01-2001,78,75.9759
)
Array
(
    [0] => 'AUD','ADF',1,'06-01-2001',3,3.9532
    [1] => 'AUD','ADP',1,'06-02-2001',99.8222,99.6682
    [2] => 'AUD','ALL',1,'06-01-2001',77,75.9759
    [3] => 'AUD','ALL',1,'06-01-2001',78,75.9759
)

Upvotes: 4

metalfight - user868766
metalfight - user868766

Reputation: 2750

Use str_replace, you can pass complete array as input :

$j = array('AUD,ADF,1,06-01-2001,3.,3.9532','AUD,ADP,1,06-02-2001,99.8222,99.6682','AUD,AED,1,06-01-2001,1.8687,1.8664','AUD,AFA,1,06-01-2001,2416.57,2413.95','AUD,AFN,1,06-01-2001,2416.57,2413.95','AUD,ALL,1,06-01-2001,77.,75.9759','AUD,AMD,1,06-03-2001,NULL,NULL','AUD,ANG,1,06-01-2001,0.9056,0.9046','AUD,AOA,1,06-01-2001,3.0751,2.9961');

$j = str_replace(".,", "," , $j);

print_r($j);

Output

Array
 (
[0] => AUD,ADF,1,06-01-2001,3,3.9532
[1] => AUD,ADP,1,06-02-2001,99.8222,99.6682
[2] => AUD,AED,1,06-01-2001,1.8687,1.8664
[3] => AUD,AFA,1,06-01-2001,2416.57,2413.95
[4] => AUD,AFN,1,06-01-2001,2416.57,2413.95
[5] => AUD,ALL,1,06-01-2001,77,75.9759
[6] => AUD,AMD,1,06-03-2001,NULL,NULL
[7] => AUD,ANG,1,06-01-2001,0.9056,0.9046
[8] => AUD,AOA,1,06-01-2001,3.0751,2.9961
)

Upvotes: 1

mmaxbb
mmaxbb

Reputation: 36

You have to use array_walk() and preg_replace() to do that. Expression for regex should be something like (.)[,\s]

Upvotes: 0

Ben Dubuisson
Ben Dubuisson

Reputation: 757

You could use intval() to transform your floating points into integers: http://php.net/manual/en/function.intval.php Looks like you might have to split the values (separated by commas), transform them and then join them again...

Upvotes: 1

Related Questions