Home  >  Article  >  Backend Development  >  How to remove null values ​​from array in php

How to remove null values ​​from array in php

PHPz
PHPzOriginal
2023-04-25 09:04:15838browse

In PHP development, arrays often need to be processed. Sometimes, there will be null values ​​in the array, and these null values ​​may interfere with our program logic or lead to wrong results. Therefore, you need to use PHP functions to remove null values ​​​​in the array to improve the robustness and stability of the program.

1. What is a null value in an array

In PHP, a null value in an array refers to the following situations:

  1. Array element value is null
  2. The array element value is false
  3. The array element value is an empty string
  4. The array element value is 0 or the string '0'
  5. Array The element does not exist

In practical applications, we need to determine which values ​​are counted as null values ​​based on specific circumstances. For example, when processing form data, an empty string may not count as a null value because the user may intentionally leave a form item blank. When calculating the average of array elements, both empty strings and 0 may be counted as null values ​​because they cannot participate in the operation.

2. Remove empty values ​​in the array

  1. Use the array_filter function

The array_filter function can filter the elements in the array and can be customized through the callback function Filter rules. For example, you can pass in the following callback function to remove empty values ​​​​in the array:

function removeEmpty($value) {
  return !empty($value) || $value === 0;
}

$array = array(1, null, 2, false, "", "0", 0);
$result = array_filter($array, 'removeEmpty');
print_r($result);

The above code will output the non-empty elements in the array:

Array
(
    [0] => 1
    [2] => 2
    [5] => 0
)

In the callback function, the empty function is used Determine whether the element is a null value, 0 is also counted as a null value. Therefore, it is necessary to determine both non-empty and 0 conditions in the return value to retain 0.

  1. Using a foreach loop

Another way to implement this is to use a foreach loop to traverse the array and remove or replace empty value elements from the array with other values. The following is a sample code:

$array = array(1, null, 2, false, "", "0", 0);
foreach ($array as $key => $value) {
  if (empty($value) && $value !== 0) {
    unset($array[$key]);
  } else {
    $array[$key] = trim($value);
  }
}
print_r($array);

In the above code, the empty function is used in the loop to determine whether the element is a null value, and 0 is also counted as a null value. Use the unset function to remove null elements from the array. Or use the trim function to replace empty strings with null or other non-null values.

3. Summary

This article introduces how to use PHP functions to remove null values ​​​​in arrays. In practical applications, it is necessary to determine which values ​​are counted as null values ​​based on specific requirements and choose an appropriate processing method. This can improve the robustness and stability of the program and reduce the occurrence of unexpected errors.

The above is the detailed content of How to remove null values ​​from array in php. 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