2011-11-05 70 views
0

我已经写了这个代码:确定,如果数组有负数,并将其更改为零

$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]) 
); 

他们中有些人将返回负数(-13-42等),如何改变负到0

通过的思维方式我这样做后:

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

例子:我需要将其转换就像下面这个:

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

回答

1

我喜欢Hakre的紧凑型答案。但是,如果你有更复杂的要求,您可以使用函数:

<?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(随着echo显示试验前$result。)

它给你:

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

注意,你也可以直接返回三元:

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

除了在循环中使用它:

<?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

+0

谢谢,干净,可以理解!这只是一个问题,我在while循环中使用它,并且第二次抛出一个错误:'不能重新声明getZeroResult()(以前在C:\ xampp \ htdocs \中声明):40 C:\ xampp \ htdocs \ 40行' – Ricardo

+0

你能帮我解决这个错误吗? – Ricardo

+0

不要在'while','foreach'或'for'循环中声明一个函数。在全局范围内声明它(不在其他任何构造中)。 –

2

猜这是功课。如果是这样,请将其标记为。

提示:

  • 使用循环,而不是硬布线阵列的引用。
  • 使用if语句检查负值并切换其符号。
  • 您的使用加入是正确的
+0

基本上我喜欢在工作时学习(不,它不是一项家庭作业)我只是建立一些脚本来获得经验。至于答案 - 我在考虑if语句,我可以这样做,但我想知道是否有一些“干净”的方式来做到这一点。关于循环,我该怎么做? – Ricardo

6

您可以映射:

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

将会把所有的数字低于0比0

array_mapmax

此外,您可以为您节省更多的笔迹与$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); 

array_reduce

编辑: foreach循环可能会更容易理解,我添加了一些意见,以及:

// 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 
} 

这里是demo

+0

看起来像我一直在寻找的东西,但似乎你忘了那里的一封信或两封信,我现在会注意到这个错误。 – Ricardo

+0

我只是输入它,代码未经测试,让我检查。 - 第二个例子是错误的,这需要另一个映射 - 现在修复。 – hakre

+0

哦,我的天哪,我认为你的答案更像是亲你喜欢你,因为我不能真正理解这里发生了什么,猜猜现在阅读文档。 – Ricardo