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

发布于 2024-08-29 05:18:54 字数 233 浏览 2 评论 0原文

示例数组

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

所有空值都应该替换为 0。我希望有人能有一种有效的方法来做到这一点,也许是我不知道的内置 PHP 函数。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你在看孤独的风景 2024-09-05 05:18:54

您可以使用 array_walk_recursive 函数,带有一个用 0 替换 null 的回调函数。

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

注意:我认为您犯了一个拼写错误,并且您的数组不仅仅包含一个字符串,而是包含几个子元素。

You could use this kind of code :

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

With the following callback functon :

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

请注意:

  • 第一个参数是通过引用传递的!
    • 这意味着修改它将会修改数组中相应的值
  • 我使用 === 运算符进行比较

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

You could use the array_walk_recursive function, with a callback function that would replace null by 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 :

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
最舍不得你 2024-09-05 05:18:54

如果单引号是无意的,并且数组具有整数和空值:

for ($i = 0; $i < count($myArray); $i++)
{
    if ($myArray[$i] == null) $myArray[$i] = 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;
}
挽心 2024-09-05 05:18:54

从 PHP7.4 及更高版本开始,您可以使用箭头函数语法对每行迭代调用 intval()。这对于您的数据来说是安全的,因为无条件地将输入值转换为 int 类型数据不会损坏任何输入值。

代码:(演示

var_export(
    array_map(fn($row) => array_map('intval', $row), $myArray)
);

From PHP7.4 and higher, you can use arrow function syntax to make iterated calls of intval() on each row. This is safe to use on your data because none of the input values will be damaged by unconditionally casting them to int type data.

Code: (Demo)

var_export(
    array_map(fn($row) => array_map('intval', $row), $myArray)
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文