I got the following array:
JavaScript
x
array(1) {
[0]=>
array(2) {
["name"]=>
string(0) ""
["fistname"]=>
NULL
}
}
I want to process it if not empty. I have tried:
JavaScript
if (!empty($data->User)) {
echo 'filled';
} else {
echo 'empty';
}
But this returns
filled
Edit after Marcin Orlowski answer
JavaScript
$user = $data->User();
var_dump($user);
if (!empty($user)) {
echo 'filled';
} else {
echo 'empty';
}
shows:
JavaScript
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
JavaScript
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,
JavaScript
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
JavaScript
public function isEmptyUser() {
return (empty($this->name) && empty($this->firstname));
}