3

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?

Neocortex
  • 653
  • 9
  • 32
  • `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 – Dave Dec 10 '13 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. – Neocortex Dec 10 '13 at 13:48

4 Answers4

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
  )
);
ThW
  • 19,120
  • 3
  • 22
  • 44
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'; }
}
Ryan
  • 3,552
  • 1
  • 22
  • 39
  • Not too sure on the reason for the downvote - care to elaborate? – Ryan Dec 10 '13 at 13:20
  • 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 Dec 10 '13 at 13:23
  • 2
    @Dave if the 'built in functions' you're referring to is the array_map, take a look at this post. http://willem.stuursma.name/2010/11/22/a-detailed-look-into-array_map-and-foreach/ - `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.` – Ryan Dec 10 '13 at 13:25
  • @Dave I disagree, there are no built in functions to do this. This answer is by far the simplest solution to the problem. – AlexP Dec 10 '13 at 13:25
  • Try it with an array with 100k+ entries then come back and say this is efficient. I don't rate array_map either just fyi – Dave Dec 10 '13 at 13:27
  • @Dave If you're going to run it with an array which includes 100K entries, you can expect a slight performance hit regardless. If you don't rate array_map, then it might be worth posting an answer which suggests a way of doing this which utilizes PHP's 'built in functions'. – Ryan Dec 10 '13 at 13:28
  • @Dave `array_map` is just a loop in another form, plus the overhead of repeated function calls, plus guaranteed data duplication instead of modification by reference. Yeah, much more efficient... – deceze Dec 10 '13 at 13:28
  • The difference is between an internal interpreter loop vs a scripted loop, the previously linked article is also many years out of date vs current though you're welcome to re-run the tests using a more current version of php and re-compare – Dave Dec 10 '13 at 13:31
  • 1
    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. – Ryan Dec 10 '13 at 13:33
2
foreach($array as $key => value){
    if(empty($value)) $array[$key] = "Some random value";
}
Canser Yanbakan
  • 3,780
  • 3
  • 39
  • 65
1

Try this,

$ar=array(" ","test"," ","test2");
$ar = array_replace($ar,
array_fill_keys(
    array_keys($ar, " "),
    "hi"
)
);
Yatin Trivedi
  • 661
  • 6
  • 9
  • 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? – mickmackusa Dec 19 '22 at 06:23