【问题标题】:Determine if the array has negative numbers and change them to zero确定数组是否有负数并将它们更改为零
【发布时间】:2011-11-05 21:47:19
【问题描述】:

我已经写了这段代码:

$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

【问题讨论】:

    标签: php arrays


    【解决方案1】:

    您可以映射:

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

    将所有小于 0 的数字设置为 0。

    请参阅 array_map 和 max。

    此外,您还可以使用$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 循环可能更容易理解,我还添加了一些 cmets:

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

    【讨论】:

    • 看起来像我要找的东西,但你好像忘记了一两个字母,我现在会注意错误。
    • 我刚刚输入,代码未经测试,让我检查一下。 - 第二个示例是错误的,这需要另一个映射 - 现在已修复。
    • 哦,我的天哪,我认为您的回答更适合像您这样的专业人士,因为我无法真正理解这里发生了什么,我猜我现在阅读文档。
    • 你想要一个循环吗? - 我会尽快将第二个示例变成一个示例 - 完成。
    【解决方案2】:

    我猜这是家庭作业。如果是这样,请标记它。

    提示:

    • 使用循环而不是硬连线数组引用。
    • 使用 if 语句检查负值并切换其符号。
    • 您对 join 的使用是正确的

    【讨论】:

    • 基本上我喜欢边工作边学习(不,这不是家庭作业)我只是在编写一些脚本来获得经验。至于答案 - 我正在考虑 if 语句,我可以做到这一点,但我想知道是否有一些“干净”的方式来做到这一点。关于循环,我该怎么做?
    【解决方案3】:

    我喜欢 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

    【讨论】:

    • 谢谢,干净易懂!这只是一个问题,我在 while 循环中使用它,它第二次抛出错误:Cannot redeclare getZeroResult() (previously declared in C:\xampp\htdocs\:40) in C:\xampp\htdocs\ on line 40
    • 不要在while、foreach 或for 循环中声明函数。在全局范围内声明它(而不是在任何其他构造中)。
    • 注意,应该说function,而不是variable。
    • @Ricardo - 另请注意,当您编辑评论时,SO 不会通知我您已编辑它。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    • 2012-05-09
    相关资源
    最近更新 更多