Ricardo
Ricardo

Reputation: 1672

Determine if the array has negative numbers and change them to zero

I have wrote this code:

$final = array(
    ($data[0] - $data[1]),
    ($data[1] - $data[2]), 
    ($data[2] - $data[3]),
    ($data[3] - $data[4]),
    ($data[4] - $data[5]), 
    ($data[5] - $data[6]) 
);

Some of them will return negative numbers (-13,-42 etc...), how to change the negative ones to 0?

By the way the think i do after is:

$data_string = join(",", $final);

Example: I need to convert it like in the following:

1,3,-14,53,23,-15 => 1,3,0,53,23,0

Upvotes: 0

Views: 2631

Answers (3)

hakre
hakre

Reputation: 197785

You can map that:

$zeroed = array_map(function($v) {return max(0, $v);}, $final);

Will set all numbers lower than 0 to 0.

See array_map and max.

Additionally, you can save you some more handwriting with $data:

$final = array_reduce($data, function($v, $w)
{
    static $last;
    if (null !== $last) $v[] = max(0, $last - $w);
    $last = $w;
    return $v;
}, array());

$data_string = join(",", $final);

See array_reduce.

Edit: A foreach loop might be easier to follow, I added some comments as well:

// go through all values of data and substract them from each other,
// negative values turned into 0:
$last = NULL; // at first, we don't have a value
$final = array(); // the final array starts empty
foreach($data as $current)
{
    $isFirst = $last === NULL; // is this the first value?
    $notFirst = !$isFirst;

    if ($notFirst)
    {
        $substraction = $last - $current;
        $zeroed = max(0, $substraction);
        $final[] = $zeroed;        
    }
    $last = $current; // set last value
}

And here is the demo.

Upvotes: 6

Jared Farrish
Jared Farrish

Reputation: 49208

I like Hakre's compact answer. However, if you have a more complex requirement, you can use a function:

<?php

$data = array(11,54,25,6,234,9,1);

function getZeroResult($one, $two) {
    $result = $one - $two;
    $result = $result < 0 ? 0 : $result;

    return $result;
}

$final = array(
    getZeroResult($data[0], $data[1]),
    getZeroResult($data[1], $data[2]), 
    getZeroResult($data[2], $data[3]),
    getZeroResult($data[3], $data[4]),
    getZeroResult($data[4], $data[5]), 
    getZeroResult($data[5], $data[6]) 
);

print_r($final);

?>

http://codepad.org/viGBYj4f (With an echo to show the $result before test.)

Which gives you:

Array
(
    [0] => 0
    [1] => 29
    [2] => 19
    [3] => 0
    [4] => 225
    [5] => 8
)

Note, you could also just return the ternary:

function getZeroResult($one, $two) {
    $result = $one - $two;
    return $result < 0 ? 0 : $result;
}

As well as using it in a loop:

<?php

$data = array(11,54,25,6,234,9,1);

function getZeroResult($one, $two) {
    $result = $one - $two;
    echo "$one - $two = $result\n";
    return $result < 0 ? 0 : $result;
}

$c_data = count($data)-1;
$final = array();

for ($i = 0; $i < $c_data; $i++) {
    $final[] = getZeroResult($data[$i], $data[$i+1]);
}

print_r($final);

?>

http://codepad.org/31WCbpNr

Upvotes: 1

Raymond Hettinger
Raymond Hettinger

Reputation: 226346

Am guessing this is homework. If so please mark it as such.

Hints:

  • use a loop instead of hardwired array references.
  • use an if-statement to check for negative values and switch their sign.
  • your use of join is correct

Upvotes: 2

Related Questions