4

I have to read a PHP array for empty values. If there are any empty values for any key, I just wanted them to be filled with some default value if empty.

1. Is there any in built function to check if empty in an array and fill it up?

(OR)

2. What is the procedure to accomplish this requirement?

3
  • array array_filter ( array $array [, callable $callback ] ) call back is a simple function to check empty() that would provide you with a list of keys with empty values in them as a result array you can then populate Commented Dec 10, 2013 at 13:14
  • Dave as we know array_filter is to remove empty values. Just wondering whether it can able to list the empty value's keys. Commented Dec 10, 2013 at 13:48
  • stackoverflow.com/questions/12830039/… Commented Sep 18, 2024 at 20:41

4 Answers 4

6

array_map() can be used to apply a mapping to each array element.

$array = array(1, 0, 'foo', '', 'bar', NULL);
$default = 'DEFAULT';

var_dump(
  array_map(
    function($value) use ($default) {
      return $value ?: $default;
    },
    $array
  )
);
Sign up to request clarification or add additional context in comments.

7 Comments

+1 Though you shouldn't use empty on variables that are guaranteed to exist. !$value will do just fine here.
You're right, this allows to use the ?: operator.
@deceze Why not empty?
@ThomasWeinert - bare in mind the use of the ternary operator without the middle part is only available since PHP 5.3
|
3

There isn't a built in function which replaces empty values.

You could loop through the array, and if the value is empty, populate it.

For example

foreach($arr as &$val) {
    if(empty($val)) { $val = 'Empty'; }
}

9 Comments

Not too sure on the reason for the downvote - care to elaborate?
stop using loops like this thats why its a waste of resources when php has built in functions for doing exactly this that and you're flat out wrong when you say "there isn't a built in function"
@Dave if the 'built in functions' you're referring to is the array_map, take a look at this post. willem.stuursma.name/2010/11/22/… - With big arrays, the peak memory usage of the array_map() function may be 2.5 times as much as the foreach loop. The fastest is the foreach construct. This construct can about 70% faster than an anonymous function, at least when there is not much to do within the loop.
@Dave I disagree, there are no built in functions to do this. This answer is by far the simplest solution to the problem.
I'm not going to even waste the time on proving such a point as I see no harm in using a foreach loop as opposed to an array map. I understand your point and until I have my own solid evidence I'm not going to argue it, but I felt you were making the point, just to make a point and that it wasn't actually a concern.
|
2
foreach($array as $key => value){
    if(empty($value)) $array[$key] = "Some random value";
}

Comments

2

Try this,

$ar=array(" ","test"," ","test2");
$ar = array_replace($ar,
array_fill_keys(
    array_keys($ar, " "),
    "hi"
)
);

1 Comment

Why "try this"? Why not explain what your answer does, why it is appropriate, and how you know that single-space strings are the appropriate targets?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.