简体   繁体   English

检查属性值是null还是空

[英]Checking if an attribute value is null or empty

I am having issue in manipulating JSON data using PHP in Oracle RightNow CRM. 我在Oracle RightNow CRM中使用PHP操作JSON数据时遇到了问题。 The sample json is included below. 样本json包含在下面。 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条件时,“person.private_email”属性中包含的数据,只有在存在某些数据时才会计算为true。 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 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 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. 要检查它是否为null, isset()就足够了,为了检查它是否为空,你最好trim()字符串,因为它删除了空格,如果用empty()函数检查空格,则空格被计为字符。

$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. 我认为你不能同时检查(&&),因为isset()将检查是否在JSON数据中设置了PERSON.PRIVATE_EMAIL,否则如果它只是空白,那么empty()就足够了。

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() : 要检查对象中是否存在属性,可以使用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 . 请注意,即使属性为null ,这也会生成true To check whether a property exists and that it's not null you use isset() : 要检查属性是否存在以及它是否为null请使用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.... 在示例代码中,您将json字符串解析为本地$ person变量,而在您的条件中,您正在检查$ person的静态类属性....

ie, $person is not the same as self::$person. 即,$ person与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. 此代码适用于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";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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