Check If array is null or not in php

Check If array is null or not in php

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Check If array is null or not in php

When parsing XML data, particularly when using SimpleXMLElement, it is common to encounter edge cases where the returned structure looks partially populated but contains no actual data. Consider a scenario where you parse an XML URL and receive an array like this:

Array
  (
    [Tags] => SimpleXMLElement Object
      (
          [0] => 
      )
  )

You need to determine if the [Tags] key exists but is effectively empty, or if the entire structure is null. The immediate problem lies in how PHP evaluates "empty" for SimpleXMLElement objects versus standard arrays.

Evaluating SimpleXMLElement Objects

Standard array checks using empty() or checking for null often fail when dealing with SimpleXMLElement instances because these objects are rarely null themselves, even if they contain no children nodes. In the example provided, $result['Tags'] is a valid SimpleXMLElement object, not null. However, iterating over it reveals that index [0] is absent or empty.

If you attempt to check this with a simple truthiness test:

if (!empty($result['Tags'])) {
    // This block will likely execute even if the array is effectively empty
}

This happens because empty() returns false only if the variable is null, 0, "0", "", [], or a false boolean. A SimpleXMLElement object does not fall into these categories unless explicitly converted or cast in a specific way. Therefore, relying solely on !empty($result['Tags']) will treat an empty XML tag set as populated data.

The Correct Approach: Iteration and Key Existence

To accurately detect if the array is null or devoid of content, you must verify two conditions separately:

  1. Does the key exist?
  2. Does the object contain any child elements?

First, ensure the key exists using isset() or array_key_exists(). If the key does not exist at all, $result['Tags'] will return null (assuming default array behavior) or trigger a notice depending on your configuration.

if (isset($result['Tags'])) {
    // The object exists, but is it populated?
}

If the key exists, you must check for child nodes. For SimpleXMLElement, the most reliable method is to count the children or attempt to access the first child and verify its truthiness in a specific context. However, a simpler pattern often works effectively by casting or checking the internal structure.

A robust way to check if the XML object contains data is to iterate over it immediately. If the loop does not run, the collection is empty:

$tags = $result['Tags'];

// Check if the object exists first
if (!isset($tags)) {
    echo "Failure: No Tags key found.";
} else {
    // Check if there are any children nodes
    $count = count((array) $tags); 
    
    if ($count === 0) {
        echo "Failure: Tags array is empty.";
    } else {
        echo "Success: Found tags.";
    }
}

Casting the SimpleXMLElement to an array using (array) allows you to use standard count() functions, which accurately reflect whether there are child nodes. This approach separates the existence of the object from the existence of its content.

Handling Potential Pitfalls

A common mistake developers make is assuming that $result['Tags'][0] returning null means the variable itself is null. In your specific XML parsing case, the structure shows [0] => (empty). This indicates the object exists but has no indices set at [0]. If you try to access $result['Tags'][0] directly without checking, PHP might return null, but the parent object remains valid.

Another pitfall is relying on is_null() or !isset(). As shown in the example, the key [Tags] is present in the array structure, so isset() will return true. If you only check for nullness, you miss the fact that the XML parser successfully created an object but found no matching tags in the source document.

Conclusion on Data Validation

When working with parsed XML structures generated from URLs, always distinguish between a missing key and an empty collection. The presence of a SimpleXMLElement object does not guarantee data. You must cast the object to an array or count its children to confirm that the dataset is actually populated before proceeding with logic that assumes specific elements exist.

For further insights into robust XML parsing strategies and handling complex data structures in PHP, you might explore resources such as https://laravelcompany.com, which often covers advanced topics in web development and data manipulation.

Our network