user3733831 user3733831 - 2 months ago 7
PHP Question

Check PHP array is empty or not?

I am having a php array like below:

echo '<pre>', print_r($empData[$employee]['banks']).'</pre>';
Array
(
[0] => Array
(
[bank_name] => []
[bank_code] =>
[branch] =>
[account_number] =>
[account_type] =>
)

)


Now I need to check, this
bank array
is empty or not. That mean all index should have a value.

This is how I tried it:

$proceed = false;
foreach ($empData[$employee]['banks'] as $key) {

echo '<pre>', print_r($key).'</pre>';

foreach ($key as $k => $v) {
if (empty($empData[$employee]['banks'][$k])){
$proceed = true;
break;
}
}
}

if(!$proceed) {
echo 'This array is empty';
}
else {
echo 'This array is not empty';
}


But, Its not working for me. Its always going to else part, even if the array is not empty.

UPDATE:

$proceed = false;
foreach ($empData[$employee]['banks'] as $bank) {
if (!array_filter($bank)) {
$proceed = true;
break;
}
}

if($proceed) {
echo 'This array is empty';
}
else {
echo 'This array is not empty';
}


Array1: Its not empty. But output says
Its not empty
. so its ok

This array is not empty

Array
(
[0] => Array
(
[bank_name] => Bank Name
[bank_code] => 7083
[branch] => Branch Name
[account_number] => 234234324242
[account_type] => NRFC
)

)


Array2: Its an empty. But output says
Its not empty
. so its wrong

This array is not empty

Array
(
[0] => Array
(
[bank_name] => []
[bank_code] =>
[branch] =>
[account_number] =>
[account_type] =>
)

)


Hope somebody may help me out.

Answer

EDIT:

Your question has been updated a couple times. It looks like you might want to consider a $bank as "empty" if one of the values are unpopulated – my original answer (below) will only return true if all values match the function. What we'd need to accommodate this is a slightly different function, array_some.

function array_some(callable $f, array $xs) {
  foreach (array_values($xs) as $x)
    if (call_user_func($f, $x))
      return true;
  return false;
}

$input = [
  [
    'bank_name' => [],
    'bank_code' => null,
    'branch' => null,
    'account_number' => null,
    'accout_type' => null
  ],
  [
    'bank_name' => ['foobar'],
    'bank_code' => 123,
    'branch' => null,
    'account_number' => null,
    'accout_type' => null
  ],
  [
    'bank_name' => 'Bank Name',
    'bank_code' => 7083,
    'branch' => 'Branch Name',
    'account_number' => '234234324242',
    'accout_type' => 'NRFC'
  ]
];

function is_empty($x) { return empty($x); }

foreach ($input as $bank) {
  $proceed = array_some('is_empty', $bank);
  echo 'proceed:', json_encode($proceed), PHP_EOL;
}

// proceed:true
// proceed:true
// proceed:false
  • In the first example, some fields are empty, so $proceed will be true
  • In the second example, some fields are empty, so $proceed will be true
  • In the third example, all fields are non-empty, so $proceed will be false

https://repl.it/DcCW


ORIGINAL ANSWER:

What you're looking for is an array_every function. Let's start with a simple example:

function array_every(callable $f, array $xs) {
  foreach (array_values($xs) as $x)
    if (!call_user_func($f, $x))
      return false;
  return true;
}

function even($x) { return $x % 2 === 0; }

echo "even:", json_encode(array_every('even', [1,2,3])), PHP_EOL; // false
echo "even:", json_encode(array_every('even', [2,4,6])), PHP_EOL; // true

https://repl.it/DcCZ

  • In the first example, 1, 2, and 3 are not all even, so the output is false.

  • In the second example, 2, 4, and 6 are all even, so the output is true.


Let's see it with your data now

function array_every(callable $f, array $xs) {
  foreach (array_values($xs) as $x)
    if (!call_user_func($f, $x))
      return false;
  return true;
}

$input = [
  [
    'bank_name' => [],
    'bank_code' => null,
    'branch' => null,
    'account_number' => null,
    'accout_type' => null
  ],
  [
    'bank_name' => ['foobar'],
    'bank_code' => 123,
    'branch' => null,
    'account_number' => null,
    'accout_type' => null
  ]
];

function is_empty($x) { return empty($x); }

foreach ($input as $bank) {
  $proceed = array_every('is_empty', $bank);
  echo 'proceed:', json_encode($proceed), PHP_EOL;
}

// proceed:true
// proceed:false

https://repl.it/DcCX

  • In the first example data, all of the array values are empty, therefore $proceed will be true.

  • In the second example, some of the values are (non-empty), therefore $proceed will be false.

Note: Because empty is a language construct and not a function, it cannot be called using variable functions. – This is why I defined a reusable is_empty function instead of using array_every('empty', ...) – which would not work.

Comments