-2

How can I find out if an array has values for specific positions AND everything else is empty?
Let's say I want to check indexes 2,3,4
If I have this array:
10,3,56,78,89,89 returns false
if I have ,,45,56,67,, returns true
How can I do this in PHP?

Pedro Lobito
  • 85,689
  • 29
  • 230
  • 253
MalcolmInTheCenter
  • 1,140
  • 5
  • 21
  • 41
  • 1
    Please **clarify your specific problem or add additional details to highlight exactly what you need**. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to Ask](http://stackoverflow.com/help/how-to-ask) page for help clarifying this question – Pedro Lobito May 03 '17 at 01:41

2 Answers2

1

Try this out (considering we're inside of function)

$allowed = [2, 3, 4];
$arr = [NULL, NULL, 45, 56, 67, NULL, NULL];
foreach($arr as $k => $v) 
  if(!empty($v) && !in_array($k, $allowed)) return false;
return true;
1

This works well for you. array compare, array_keys(), array_filter()

return [2,3,4] == array_keys(array_filter($array)) ? true : false;
Community
  • 1
  • 1
LF00
  • 24,667
  • 25
  • 136
  • 263