How do you check if the array of objects is empty in php?
Edit: I didn't realize they wanted to specifically check if a SimpleXMLElement object is empty. I left the old answer below Show Updated Answer (SimpleXMLElement):For SimpleXMLElement: If by empty you mean has no properties:
OR
If SimpleXMLElement is one level deep, and by empty you actually mean that it only has properties PHP considers falsey (or no properties):
If SimpleXMLElement is more than one level deep, you can start by converting it to a pure array:
Old Answer (simple object):If you want to check if a simple object (type
Source: http://php.net/manual/en/language.oop5.object-comparison.php Edit: added example (PHP 4, PHP 5, PHP 7, PHP 8) empty — Determine whether a variable is empty Descriptionempty(mixed Parametersvar Variable to be checked No warning is generated if the variable does not exist. That means empty() is essentially the concise equivalent to !isset($var) || $var == false. Return Values Returns ExamplesExample #1 A simple empty() / isset() comparison.
Example #2 empty() on String Offsets
The above example will output: bool(true) bool(false) bool(false) bool(false) bool(true) bool(true) Notes
See Also
Nanhe Kumar ¶ 8 years ago
$testCase as $k => $v) { Janci ¶ 13 years ago
steven at nevvix dot com ¶ 11 years ago
info at ensostudio dot ru ¶ 1 year ago
markmanning at gmail dot com ¶ 3 years ago
"Time taken: " . (microtime(true) - $ts) . " sec\n"; your dot brother dot t at hotmail dot com ¶ 7 years ago
Glass { martin dot aarhof at gmail dot com ¶ 10 years ago
Anonymous ¶ 14 years ago
chrisdmiddleton at gmail dot com ¶ 8 years ago
$var); wranvaud at gmail dot com ¶ 5 years ago
Claudio Galdiolo ¶ 5 years ago
phpsort ¶ 11 years ago
denobasis-bozic et yahoo.com ¶ 13 years ago
$testCase as $key => $case ) {
tom at tomwardrop dot com ¶ 14 years ago
marko dot crni at gmail dot com ¶ 12 years ago
chris dot wisefool at gmail dot com ¶ 11 years ago
aidan1103 at yahoo dot com ¶ 12 years ago
mlibazisi mabandla ¶ 13 years ago
ehsmeng ¶ 12 years ago
paul at worldwithoutwalls dot co dot uk ¶ 18 years ago
mcfogw at gmail dot com ¶ 6 years ago
thomas at thomasnoest dot nl ¶ 13 years ago
e dot klerks at i-bytes dot nl ¶ 11 years ago
fahimcseiiuc at gmail dot com ¶ 3 years ago
Greg Hartwig ¶ 14 years ago
EllisGL ¶ 14 years ago
jmarbas at hotmail dot com ¶ 17 years ago
rodolphe dot bodeau at free dot fr ¶ 11 years ago
qeremy ¶ 10 years ago
rkulla2 at gmail dot com ¶ 15 years ago
Lukas ¶ 4 months ago
$testCase as $k => $v) { GazetteSDF ¶ 4 years ago
Javier Alfonso ¶ 6 years ago
$a['key'])?'A empty':'A not empty'), ' - ', (empty($b['key'])?'B empty':'B not empty'), ' - ', (empty($c['key'])?'C empty':'C not empty'), ' - ', (empty($d['key'])?'D empty':'D not empty'); xzero at elite7hackers dot net ¶ 5 years ago
aditycse at gmail dot com ¶ 4 years ago
class EmptyClass { How check if object is empty array?Having confirmed that the variable is an array, now we can check the length of the array using the Array. length property. If the length of the object is 0, then the array is considered to be empty and the function will return TRUE. Else the array is not empty and the function will return False.
How do you check PHP object is empty or not?PHP empty() Function
The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.
Is array empty in PHP?php $emptyArray = array(); $isEmpty = empty($emptyArray); echo("The function has returned $isEmpty.
...
Use empty() Function to Check Whether an Array Is Empty in PHP.. Is an empty array empty PHP?An empty array is falsey in PHP, so you don't even need to use empty() as others have suggested. PHP's empty() determines if a variable doesn't exist or has a falsey value (like array() , 0 , null , false , etc).
|