How to Array Replace Empty Values With Null in PHP?

03-Apr-2023

.

Admin

How to Array Replace Empty Values With Null in PHP?

Hi Dev,

This simple article of how to array replace empty values with null in php. This tutorial will give you example of replace all null values in an array in php. this example will help you empty string with nulls replacing in array php. you will learn array_map() function use by rplace empty value with null. Follow bellow tutorial step of replacing empty value with null using php.

To replace all occurrences of null in an array with another value you can use the array_map() function that is built into PHP.

Example 1:


index.php

<?php

$replace = '';

$values = ['x', 'y', null, 'z'];

$result = array_map(fn ($value) => $value ?? $replace,$values);

print_r($result);

?>

Output:

Array

(

[0] => x

[1] => y

[2] =>

[3] => z

)

I hope it could help you...

#PHP