php 添加到数组值 (+1)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3478227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:55:01  来源:igfitidea点击:

Add to an array value (+1)

php

提问by Steven

I have an array

我有一个数组

$hourly = array(
        "01" => "0",
        "02" => "0",
        "03" => "0",
        "04" => "0",
        "05" => "0",
        "06" => "0",
        "07" => "0",
        "08" => "0",
        "09" => "0",
        "10" => "0",
        "11" => "0",
        "12" => "0",
        "13" => "0",
        "14" => "0",
        "15" => "0",
        "16" => "0",
        "17" => "0",
        "18" => "0",
        "19" => "0",
        "20" => "0",
        "21" => "0",
        "22" => "0",
        "23" => "0"
            );

And I have a bunch of data like "01" and "03" and "21" and I want to add (+1) to that specific value in the array. So with the data set "01","03","21","01","22" the resulting array would be

我有一堆数据,如“01”、“03”和“21”,我想将 (+1) 添加到数组中的特定值。因此,对于数据集“01”、“03”、“21”、“01”、“22”,结果数组将是

$hourly = array(
        "01" => "2",
        "02" => "0",
        "03" => "1",
        "04" => "0",
        "05" => "0",
        "06" => "0",
        "07" => "0",
        "08" => "0",
        "09" => "0",
        "10" => "0",
        "11" => "0",
        "12" => "0",
        "13" => "0",
        "14" => "0",
        "15" => "0",
        "16" => "0",
        "17" => "0",
        "18" => "0",
        "19" => "0",
        "20" => "0",
        "21" => "1",
        "22" => "1",
        "23" => "0"
            );

How could I go about doing that? Is there a function to add 1 to an array element?

我怎么能这样做呢?是否有将1添加到数组元素的函数?

回答by Scott

$updates = array("01","03","21","01","22");
foreach($updates as $num) {
    $hourly[$num]++;
}

回答by Mark Baker

$hours = array("01", "03", "21" );
foreach($hours as $hour) {
   $hourly[$hour] += 1;
}

回答by Artefacto

Normally, you'd be able to do:

通常,您可以执行以下操作:

$array["key"]++;

However, your arrays have a few peculiarities you should fix;

但是,您的阵列有一些您应该修复的特性;

  • Your key values are actually strings. If you want numbers you can increment, you should use numbers from the beginning. If you store a string and increment it with the syntax above, it'll be turned into an integer.
  • Arrays store string or a numbers as keys. You're using both. "01" will be stored as a string key, "10" will be stored as a number. Consider storing only numbers as keys.
  • 您的键值实际上是字符串。如果你想要可以递增的数字,你应该从一开始就使用数字。如果您存储一个字符串并使用上面的语法增加它,它将变成一个整数。
  • 数组存储字符串或数字作为键。你两个都用。“01”将存储为字符串键,“10”将存储为数字。考虑只存储数字作为键。

None of these make your script not work, but the inconsistency and unnecessary performance hit are avoidable.

这些都不会使您的脚本无法工作,但可以避免不一致和不必要的性能损失。

回答by rubber boots

You can use a completely "functional" approach, even if it's PHPand even id its not very beautiful ;-) But it works (PHP >= 5.3.0):

您可以使用完全“功能性”的方法,即使它是 PHP,甚至 id 它也不是很漂亮;-) 但它有效(PHP >= 5.3.0):

...

$fr = 1; $to = 23;

# generate original Array
$hourly = array_combine(
  array_map( function($n){return sprintf("%02s",$n);}, range($fr,$to) ), # Keys
  array_map( function($n){return 0;}, range($fr,$to) )                   # Values
);

$updates = Array('01','03','21','01','22');

# update values based on keys found in $updates
array_walk( $updates, function($u){global $hourly; $hourly[$u]++;} );

...

Regards

问候

rbo

红包

回答by John Parker

If you simply want to increment each element in the array, you could use:

如果您只想增加数组中的每个元素,您可以使用:

<?php

    foreach($hourly as &$element) {
        $element++;
    }

?>

However, if you only want to increment elements that hold a value, you can use:

但是,如果您只想增加包含值的元素,则可以使用:

<?php

    foreach($hourly as &$element) {
        $element = $element ? $element + 1 : $element;
    }

?>

回答by Alan Pearce

You could construct an array with the updated info ($new = array('01' => '02','03' => '01',)etc and then use array_replace($hourly,$new)or array_merge($hourly,$new). There are probably alternative functions too.

您可以使用更新的信息($new = array('01' => '02','03' => '01',)等)构造一个数组,然后使用array_replace($hourly,$new)or array_merge($hourly,$new)。也可能有替代函数。