Home  >  Article  >  Backend Development  >  PHP two-dimensional array removes null values

PHP two-dimensional array removes null values

王林
王林Original
2023-05-07 17:23:101038browse

In PHP development, we often use two-dimensional arrays. A two-dimensional array is an array with multiple arrays, each of which can contain a different number of elements. But sometimes we need to remove certain null values ​​​​in a two-dimensional array. In this case, we can use some simple methods to achieve it.

Method 1: Use foreach to loop through the array

We can use foreach to loop through the two-dimensional array, and then use the array_filter function to remove null values ​​​​in each array. Here is a sample code:

<?php

// 定义二维数组
$array = array(
  array('a', 'b', '', 'c'),
  array('', 'd', 'e'),
  array('f', '', 'g', 'h', 'i')
);

// 使用 foreach 循环遍历数组
foreach ($array as $key => $value) {
  // 使用 array_filter 函数去除每个数组中的空值
  $array[$key] = array_filter($value);
}

// 打印处理后的二维数组
print_r($array);

?>

Output result:

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
            [3] => c
        )

    [1] => Array
        (
            [1] => d
            [2] => e
        )

    [2] => Array
        (
            [0] => f
            [2] => g
            [3] => h
            [4] => i
        )

)

In this example, we use the foreach loop to traverse the two-dimensional array. In each loop, use the array_filter function to remove null values ​​in each subarray. Finally, the processed two-dimensional array is returned.

Method 2: Use the array_map function to traverse the array

We can also use the array_map function to traverse the two-dimensional array, and then use the array_filter function to remove null values ​​​​in each array. Here is a sample code:

<?php

// 定义二维数组
$array = array(
  array('a', 'b', '', 'c'),
  array('', 'd', 'e'),
  array('f', '', 'g', 'h', 'i')
);

// 使用 array_map 函数遍历数组
$array = array_map(function($value) {
  // 使用 array_filter 函数去除每个数组中的空值
  return array_filter($value);
}, $array);

// 打印处理后的二维数组
print_r($array);

?>

Output result:

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
            [3] => c
        )

    [1] => Array
        (
            [1] => d
            [2] => e
        )

    [2] => Array
        (
            [0] => f
            [2] => g
            [3] => h
            [4] => i
        )

)

In this example, we use the array_map function to traverse a two-dimensional array. In each loop, use the array_filter function to remove null values ​​in each subarray. Finally, the processed two-dimensional array is returned.

Both methods can achieve the operation of removing null values ​​in a two-dimensional array. Which method to use depends on specific needs and writing habits. In actual development, we can make choices based on code logic and efficiency.

The above is the detailed content of PHP two-dimensional array removes null values. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn