简体   繁体   English

给定一个数组数组,如何将所有空值替换为0?

[英]Given an array of arrays, how can I replace all empty values with 0?

Example array 示例数组

$myArray[0] = array('23', null, '43', '12');
$myArray[1] = array(null, null, '53', '19');
$myArray[2] = array('12', '13', '14', null);

All nulls should be replaced with 0. I was hoping someone would have an efficient way of doing this, perhaps a built in PHP function that I am unaware of. 所有的空值都应替换为0。我希望有人能够以一种有效的方式进行此操作,也许是我不知道的内置PHP函数。

You could use the array_walk_recursive function, with a callback function that would replace null by 0 . 您可以使用array_walk_recursive函数,以及将null替换为0的回调函数。


For example, considering your array is declared this way : 例如,考虑数组的声明方式如下:

$myArray[0] = array(23, null, 43, 12);
$myArray[1] = array(null, null, 53, 19);
$myArray[2] = array(12, 13, 14, null);

Note : I supposed you made a typo, and your arrays are not containing only a string, but several sub-elements. 注意:我以为您输入错误,并且数组不只包含一个字符串,而是几个子元素。


You could use this kind of code : 您可以使用以下代码:

array_walk_recursive($myArray, 'replacer');
var_dump($myArray);


With the following callback functon : 使用以下回调functon:

function replacer(& $item, $key) {
    if ($item === null) {
        $item = 0;
    }
}

Note that : 注意 :

  • the first parameter is passed by reference ! 第一个参数通过引用传递!
    • which means modifying it will modify the corresponding value in your array 这意味着修改它会修改数组中的相应值
  • I'm using the === operator for the comparison 我正在使用===运算符进行比较


And you'd get the following output : 然后您将获得以下输出:

array
  0 => 
    array
      0 => int 23
      1 => int 0
      2 => int 43
      3 => int 12
  1 => 
    array
      0 => int 0
      1 => int 0
      2 => int 53
      3 => int 19
  2 => 
    array
      0 => int 12
      1 => int 13
      2 => int 14
      3 => int 0

If the single quotation marks are unintentional, and the arrays have integers and null values: 如果单引号是无意的,并且数组具有整数和空值:

for ($i = 0; $i < count($myArray); $i++)
{
    if ($myArray[$i] == null) $myArray[$i] = 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在PHP中替换数组数组中的空白或空值 - How to replace blank or empty values in array of arrays in PHP 给定一个关联数组,其中值是平面数组,如何搜索值并返回键? - Given an associative array where the values are flat arrays, how can I search the values and return the key? 在PHP中以及在foreach循环中,如何用字符串替换空数组值? - In PHP and during a foreach loop, how can I replace empty array values with a string? 如何查找和替换数组中的空值? - How to find and replace empty values in an array? 如何在PHP中合并多维数组,并用新值替换旧值? - How can I merge multiple dimensional arrays in PHP and replace the old values with the new values? 如何将数组键复制到另一个数组,并用空数组替换值? - How to copy array keys to another array, and replace values with empty array? 如何替换多维数组中的所有特殊字符? - How can I replace all special characters in a multidimensional array? 如何检查多维数组中的所有值是否为空 - How to check if all values in multidimensional array are empty 给定一个数组数组,如何从每个值中去掉子串“GB”? - Given an array of arrays, how can I strip out the substring “GB” from each value? 用默认值替换数组中的空值 - Replace empty values in an array with defaults
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM