Home  >  Article  >  Backend Development  >  How to deal with empty arrays after merging PHP arrays?

How to deal with empty arrays after merging PHP arrays?

WBOY
WBOYOriginal
2024-04-28 13:51:01588browse

When using array_merge() in PHP to merge arrays, containing empty strings or empty arrays will produce confusing results. Solution: 1. Use array_filter() to filter null values. 2. For cases containing empty arrays, use the recursive merge function array_merge_recursive_distinct() to maintain a consistent array structure.

How to deal with empty arrays after merging PHP arrays?

Array merge when dealing with empty arrays in PHP

In PHP, use array_merge() When the function merges arrays, the end result can be confusing if one or more of the arrays contains empty elements.

Case 1: Merging arrays containing empty strings

$arr1 = [1, 2, 3];
$arr2 = [4, 5, ''];

$merged = array_merge($arr1, $arr2);

var_dump($merged);

Output:

array(6) {
  [0] => int(1)
  [1] => int(2)
  [2] => int(3)
  [3] => int(4)
  [4] => int(5)
  [5] => string(0) ""
}

As you can see, the empty strings are retained in the merge in the subsequent array.

Solution: Use array_filter() to filter null values

To remove null values ​​before merging, you can use array_filter() Function:

$arr1 = [1, 2, 3];
$arr2 = [4, 5, ''];

$arr1 = array_filter($arr1);
$arr2 = array_filter($arr2);

$merged = array_merge($arr1, $arr2);

var_dump($merged);

Output:

array(5) {
  [0] => int(1)
  [1] => int(2)
  [2] => int(3)
  [3] => int(4)
  [4] => int(5)
}

The empty string has been filtered, and the merged array does not contain empty values.

Case 2: Merge of arrays containing empty arrays

If the array contains empty arrays, a multidimensional array may be unexpectedly produced after merging:

$arr1 = [1, 2, 3];
$arr2 = [4, 5, []];

$merged = array_merge($arr1, $arr2);

var_dump($merged);

Output:

array(6) {
  [0] => int(1)
  [1] => int(2)
  [2] => int(3)
  [3] => int(4)
  [4] => int(5)
  [5] => array(0) {
  }
}

The empty array constitutes one element in the merged array, causing the result to be a multidimensional array.

Solution: Use recursive merging

To solve this problem, you can use the recursive merging method, which treats empty arrays as ordinary elements for merging:

function array_merge_recursive_distinct(array &$array1, array &$array2)
{
    $merged = $array1;

    foreach ($array2 as $key => &$value) {
        if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
            $merged[$key] = array_merge_recursive_distinct($merged[$key], $value);
        } else {
            $merged[$key] = $value;
        }
    }

    return $merged;
}

$arr1 = [1, 2, 3];
$arr2 = [4, 5, []];

$merged = array_merge_recursive_distinct($arr1, $arr2);

var_dump($merged);

Output:

array(6) {
  [0] => int(1)
  [1] => int(2)
  [2] => int(3)
  [3] => int(4)
  [4] => int(5)
  [5] => NULL
}

The empty array has been converted to NULL, and the merged array structure remains consistent.

The above is the detailed content of How to deal with empty arrays after merging PHP arrays?. 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