Skip to content
Advertisement

Check if an array of array is empty

I got the following array:

array(1) {
  [0]=>
  array(2) {
    ["name"]=>
    string(0) ""
    ["fistname"]=>
    NULL
  }
}

I want to process it if not empty. I have tried:

if (!empty($data->User)) {
   echo 'filled';
} else {
   echo 'empty';
}

But this returns

filled

Edit after Marcin Orlowski answer

$user = $data->User();
var_dump($user);
if (!empty($user)) {
   echo 'filled';
} else {
   echo 'empty';
}

shows:

array(1) {
  [0]=>
  array(2) {
    ["name"]=>
    string(0) ""
    ["fistname"]=>
    NULL
  }
}

Advertisement

Answer

As far as I understand in your code the function User() of your object $data returns your mentioned array. If this is the case then $data->User() (i.e. your array) is for sure non empty since it has one entry which is the array

array(2) {
    ["name"]=>
    string(0) ""
    ["fistname"]=>
    NULL
  }

If you want to check if the ‘name’ and ‘firstname’ properties are empty then you need a piece of code like this,

if (!empty($data->User[0]['name']) or !empty($data->User[0]['firstname'])) {
   echo 'filled';
} else {
   echo 'empty';
}

or even better check it in your class, i.e. by writing a piece of code like this in the class for $data

public function isEmptyUser() {
    return (empty($this->name) && empty($this->firstname));
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement