在PHP中重置/删除数组中的所有值

时间:2022-08-16 12:41:49

I have an array_flipped array that looks something like:

我有一个array_fliparray它看起来像:

{ "a" => 0, "b" => 1, "c" => 2 }

Is there a standard function that I can use so it looks like (where all the values are set to 0?):

是否有一个我可以使用的标准函数让它看起来像(所有的值都被设置为0):

{ "a" => 0, "b" => 0, "c" => 0 }

I tried using a foreach loop, but if I remember correctly from other programming languages, you shouldn't be able to change the value of an array via a foreach loop.

我尝试过使用foreach循环,但是如果我从其他编程语言中记得正确的话,您不应该能够通过foreach循环更改数组的值。

foreach( $poll_options as $k => $v )
  $v = 0; // doesn't seem to work...

tl; dr: how can I set all the values of an array to 0? Is there a standard function to do this?

tl;dr:如何将数组的所有值设为0?有一个标准函数来做这个吗?

9 个解决方案

#1


7  

You can use a foreach to reset the values;

可以使用foreach重置值;

foreach($poll_options as $k => $v) {
  $poll_options[$k] = 0;
}

#2


24  

$array = array_fill_keys(array_keys($array), 0);

or

array_walk($array, create_function('&$a', '$a = 0;'));

#3


6  

array_fill_keys function is easiest one for me to clear the array. Just use like

array_fill_keys函数是清除数组最简单的函数。只使用像

array_fill_keys(array_keys($array), "")

or

array_fill_keys(array_keys($array), whatever you want to do)

foreach may cause to decrease your performance to be sure that which one is your actual need.

因为每一个都可能导致你的表现下降,以确保哪一个是你的实际需要。

#4


5  

Run your loop like this, it will work:

像这样运行你的循环,它将会工作:

foreach( $poll_options as $k => $v )
    $poll_options[$k] = 0;

Moreover, ideally you should not be able to change the structure of the array while using foreach, but changing the values does no harm.

此外,在理想情况下,不应该在使用foreach时更改数组的结构,但是更改值不会有什么害处。

#5


4  

As of PHP 5.3 you can use lambda functions, so here's a functional solution:

对于PHP 5.3,您可以使用lambda函数,因此这里有一个功能解决方案:

$array = array_map(function($v){ return 0; }, $array);

#6


4  

You have to use an ampersand...

你必须使用&号…

foreach( $poll_options as &$v)
  $v = 0;

Or just use a for loop.

或者使用for循环。

#7


3  

you can try this

你可以试试这个

foreach( $poll_options as $k => &$v )
    $v = 0; 

Address of $v

v美元的地址

#8


2  

array_combine(array_keys($array), array_fill(0, count($array), 0))

Would be the least manual way of doing it.

这是最不需要手工的方法。

#9


0  

There are two types for variable assignment in PHP,

在PHP中有两种类型的变量赋值,

  1. Copy
  2. 复制
  3. Reference
  4. 参考

In reference assignment, ( $a = &$b ), $a and $b both, refers to the same content. ( read manual )

在引用转让($a = $b)中,$a和$b都指相同的内容。(手动)

So, if you want to change the array in thesame time as you doing foreach on it, there are two ways :

所以,如果你想在相同的时间内改变数组就像你对每个数组做的那样,有两种方法:

1- Making a copy of array :

foreach($array as $key=>$value){
        $array[$key] = 0 ; // reassign the array's value
}

2 - By reference:

foreach($array as $key => &$value){
        $value = 0 ;
}

#1


7  

You can use a foreach to reset the values;

可以使用foreach重置值;

foreach($poll_options as $k => $v) {
  $poll_options[$k] = 0;
}

#2


24  

$array = array_fill_keys(array_keys($array), 0);

or

array_walk($array, create_function('&$a', '$a = 0;'));

#3


6  

array_fill_keys function is easiest one for me to clear the array. Just use like

array_fill_keys函数是清除数组最简单的函数。只使用像

array_fill_keys(array_keys($array), "")

or

array_fill_keys(array_keys($array), whatever you want to do)

foreach may cause to decrease your performance to be sure that which one is your actual need.

因为每一个都可能导致你的表现下降,以确保哪一个是你的实际需要。

#4


5  

Run your loop like this, it will work:

像这样运行你的循环,它将会工作:

foreach( $poll_options as $k => $v )
    $poll_options[$k] = 0;

Moreover, ideally you should not be able to change the structure of the array while using foreach, but changing the values does no harm.

此外,在理想情况下,不应该在使用foreach时更改数组的结构,但是更改值不会有什么害处。

#5


4  

As of PHP 5.3 you can use lambda functions, so here's a functional solution:

对于PHP 5.3,您可以使用lambda函数,因此这里有一个功能解决方案:

$array = array_map(function($v){ return 0; }, $array);

#6


4  

You have to use an ampersand...

你必须使用&号…

foreach( $poll_options as &$v)
  $v = 0;

Or just use a for loop.

或者使用for循环。

#7


3  

you can try this

你可以试试这个

foreach( $poll_options as $k => &$v )
    $v = 0; 

Address of $v

v美元的地址

#8


2  

array_combine(array_keys($array), array_fill(0, count($array), 0))

Would be the least manual way of doing it.

这是最不需要手工的方法。

#9


0  

There are two types for variable assignment in PHP,

在PHP中有两种类型的变量赋值,

  1. Copy
  2. 复制
  3. Reference
  4. 参考

In reference assignment, ( $a = &$b ), $a and $b both, refers to the same content. ( read manual )

在引用转让($a = $b)中,$a和$b都指相同的内容。(手动)

So, if you want to change the array in thesame time as you doing foreach on it, there are two ways :

所以,如果你想在相同的时间内改变数组就像你对每个数组做的那样,有两种方法:

1- Making a copy of array :

foreach($array as $key=>$value){
        $array[$key] = 0 ; // reassign the array's value
}

2 - By reference:

foreach($array as $key => &$value){
        $value = 0 ;
}