简体   繁体   中英

Checking if an attribute value is null or empty

I am having issue in manipulating JSON data using PHP in Oracle RightNow CRM. The sample json is included below. When I try to check in an if condition, the data contained in the "person.private_email" attribute, it only evaluates to true when there is some data. If there is no data for that attribute, it does nothing. I am not getting any error at all. What would be the best way to check if any attribute contains no data.

JSON

{
    "PERSON.PERSON_ID": 272839,
    "PERSON.Surname": "FirstName",
    "PERSON.Given_Names": "LastName",
    "PERSON.TITLE": "MR",
    "PERSON.BIRTH_DT": "10/JUL/14",
    "PERSON.GENDER": "M",
    "PERSON.CDU_EMAIL": "S272839@mydomain.com",
    "PERSON.PRIVATE_EMAIL": ""
}

PHP

self::$person=json_decode($json);

if (isset(self::$person->{'PERSON.PRIVATE_EMAIL'}) && !empty(self::$person->{'PERSON.PRIVATE_EMAIL'}))

To check whether it is null or not, isset() is enough, for checking whether it is empty or not, you better trim() the string as it removes spaces and spaces are counted as character if you check emptiness with empty() function.

$email = self::$person->{'PERSON.PRIVATE_EMAIL'};
if (isset($email) && trim($email) != '')
    echo "Contains data";

I think you cannot combine both checking (&&) because isset() will check if PERSON.PRIVATE_EMAIL is set in JSON data, otherwise if it just blank, then empty() will suffice.

maybe should break it down to something like this:

$email = "";

if (isset(self::$person->{'PERSON.PRIVATE_EMAIL'}))
{
   if(!empty(self::$person->{'PERSON.PRIVATE_EMAIL'}))
   {
       $email = self::$person->{'PERSON.PRIVATE_EMAIL'};
   }
else
{
   //email is not set in the json data
   //do something
}

To check whether a property exists in an object you can use property_exists() :

if (property_exists(self::$person, 'PERSON.PRIVATE_EMAIL')) {
    // property exists
    $value = self::$person->{'PERSON.PRIVATE_EMAIL'};
}

Note that this will yield true even if the property is null . To check whether a property exists and that it's not null you use isset() :

if (isset(self::$person->{'PERSON.PRIVATE_EMAIL'})) {
    // property exists and is not null
}

To check whether something is empty, it's best to define what empty means; if the properties can only be strings, the empty definition could be something like:

if (strlen(trim($value))) {
    // string contains at least on non-space
}

In your sample code, you are parsing the json string into a local $person variable, while in your conditionals you are checking a static class property of $person....

ie, $person is not the same as self::$person. Unless there is more code that you are not sharing, this might be your problem.

This code works for me in RightNow CX w/ CP2.

    $json = '{
        "PERSON.PERSON_ID": 272839,
        "PERSON.Surname": "FirstName",
        "PERSON.Given_Names": "LastName",
        "PERSON.TITLE": "MR",
        "PERSON.BIRTH_DT": "10/JUL/14",
        "PERSON.GENDER": "M",
        "PERSON.CDU_EMAIL": "S272839@mydomain.com",
        "PERSON.PRIVATE_EMAIL": ""
    }';

    $person=json_decode($json);

    if(!empty($person->{'PERSON.PRIVATE_EMAIL'}))
    {
        echo "Not Empty";
    }
    else
    {
        echo "IS Empty";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM