首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP从对象中删除空项

PHP从对象中删除空项
EN

Stack Overflow用户
提问于 2018-05-05 01:17:34
回答 3查看 1.4K关注 0票数 3

我有以下对象,在这里打印为数组。此对象是从SOAP请求构建的。

AdminInfo Object (
  [Supplier] => Supplier Object (
      [Party] => Party Object (
          [OrgInfo] => OrgInfo Object (
              [CompanyName] => Bobs
              [IDInfo] => Array of IDInfo Objects (
                  [0] => IDInfo Object (
                      [IDQualifierCode] => 
                      [IDNum] => 
                    )
                  [1] => IDInfo Object (
                      [IDQualifierCode] => CompanyID
                      [IDNum] => 83e26599-d40g-4cba-9791-7d7c83de282c
                    )
                  [2] => IDInfo Object (
                      [IDQualifierCode] => TID
                      [IDNum] => BOBTID01020304
                    )
                  [3] => IDInfo Object (
                      [IDQualifierCode] => Token
                      [IDNum] => c784570e-044d-42c8-98fe-af9f7c1747f5
                    )
                )
            )
          [ContactInfo] => ContactInfo Object (
              [ContactJobTitle] => 
              [Communications] => Comm Object (
                  [CommQualifier] => 
                  [CommPhone] => 
                  [CommEmail] => 
                  [Address] => Address Object (
                      [Address1] => 
                      [Address2] => 
                      [City] => 
                      [StateProvince] => 
                      [PostalCode] => 
                      [CountryCode] => 
                    )
                )
              [ContactName] => PersonName Object (
                  [FirstName] => 
                  [MiddleName] => 
                  [LastName] => 
                )
            )
        )
    )
  [Company] => Company Object (
      [Party] => Party Object (
          [OrgInfo] => OrgInfo Object (
              [CompanyName] => SF
              [IDInfo] => 
            )
          [ContactInfo] => 
        )
    )
  [Facility] => Facility Object (
      [Party] => Party Object (
          [OrgInfo] => Array of OrgInfo Objects (
            )
          [ContactInfo] => ContactInfo Object (
              [ContactJobTitle] => Owner
              [Communications] => Array of Comm Objects(
                  [0] => Comm Object (
                        [CommQualifier] => WP
                        [CommPhone] => 1234567890
                    )
                  [1] => Comm Object (
                        [CommQualifier] => SA
                        [Address] => Address Object (
                            [Address1] => 123 NE 14th St 
                            [City] => Nowhere
                            [StateProvince] => ND
                            [PostalCode] => 12345
                            [CountryCode] => US
                          )
                      )
                  [2] => 
                  [3] => 
                )
              [ContactName] => PersonName Object (
                  [FirstName] => Bob
                  [MiddleName] => 
                  [LastName] => Tester
                )
            )
        )
    )
)

我想要做的是删除所有的空元素,并随此对象一起返回

AdminInfo Object (
  [Supplier] => Supplier Object (
      [Party] => Party Object (
          [OrgInfo] => OrgInfo Object (
              [CompanyName] => Bobs
              [IDInfo] => Array of IDInfo Objects (
                  [0] => IDInfo Object (
                      [IDQualifierCode] => 
                      [IDNum] => 
                    )
                  [1] => IDInfo Object (
                      [IDQualifierCode] => CompanyID
                      [IDNum] => 83e26599-d40g-4cba-9791-7d7c83de282c
                    )
                  [2] => IDInfo Object (
                      [IDQualifierCode] => TID
                      [IDNum] => BOBTID01020304
                    )
                  [3] => IDInfo Object (
                      [IDQualifierCode] => Token
                      [IDNum] => c784570e-044d-42c8-98fe-af9f7c1747f5
                    )
                )
            )
        )
    )
  [Company] => Company Object (
      [Party] => Party Object (
          [OrgInfo] => OrgInfo Object (
              [CompanyName] => SF
            )
        )
    )
  [Facility] => Facility Object (
      [Party] => Party Object (
          [ContactInfo] => ContactInfo Object (
              [ContactJobTitle] => Owner
              [Communications] => Array of Comm Objects (
                  [0] => Comm Object (
                        [CommQualifier] => WP
                        [CommPhone] => 1234567890
                    )
                  [1] => Comm Object (
                        [CommQualifier] => SA
                        [Address] => Address Object (
                            [Address1] => 123 NE 14th St 
                            [City] => Nowhere
                            [StateProvince] => ND
                            [PostalCode] => 12345
                            [CountryCode] => US
                          )
                      )
                )
              [ContactName] => PersonName Object (
                  [FirstName] => Bob
                  [LastName] => Tester
                )
            )
        )
    )
)

这些尝试根本没有做到这一点;变量$AdminInfo是上面的对象...

来自此处的解决方案:strip null values of json object

$json = json_encode($AdminInfo);
$result = preg_replace('/,\s*"[^"]+":null|"[^"]+":null,?/', '', $json);
$echo $result;

来自此处的解决方案:How to remove null values from an array?

$json = json_encode($AdminInfo);                // convert to JSON
$arr = (array)json_decode($json);               // convert to an array
$object = (object) array_filter((array) $arr);  // filter the array
$result = json_encode($object);                 // convert it back to JSON
echo $result;

从这里:PHP - How to remove empty entries of an array recursively?

function array_remove_empty($haystack)
{
    foreach ($haystack as $key => $value) {
        if (is_array($value)) {
            $haystack[$key] = array_remove_empty($haystack[$key]);
        }

        if (empty($value)) {
            unset($haystack[$key]);
        }
    }

    return $haystack;
}

$json = json_encode($AdminInfo);          // convert to JSON
$arr = (array)json_decode($json);         // convert to an array
print_r(array_remove_empty($arr));        // run through array_remove_empty function and print

来自此处的解决方案:Remove items from multidimensional array in PHP

function cleanArray($array) {
    if (is_array($array)) {
        foreach ($array as $key => $sub_array)
        {
            $result = cleanArray($sub_array);
            if ($result === false) {
                unset($array[$key]);
            } else {
                $array[$key] = $result;
            }
        }
    }
    if (empty($array)) {
        return false;
    }
    return $array;
}

$json = json_encode($AdminInfo);          // convert to JSON
$arr = (array)json_decode($json);         // convert to an array
print_r(cleanArray($arr));                // run through cleanArray function and print

编辑

作为JSON的AdminInfo对象:

{
    "Supplier":{
        "Party":{
            "OrgInfo":{
                "CompanyName":"Bobs",
                "IDInfo":[
                    {
                        "IDQualifierCode":null,
                        "IDNum":""
                    },
                    {
                        "IDQualifierCode":"CompanyID",
                        "IDNum":"83e26599-d40g-4cba-9791-7d7c83de282c"
                    },
                    {
                        "IDQualifierCode":"TID",
                        "IDNum":"BOBTID01020304"
                    },
                    {
                        "IDQualifierCode":"Token",
                        "IDNum":"c784570e-044d-42c8-98fe-af9f7c1747f5"
                    }
                ]
            },
            "ContactInfo":{
                "ContactJobTitle":"",
                "Communications":{
                    "CommQualifier":null,
                    "CommPhone":"",
                    "CommEmail":"",
                    "Address":{
                        "Address1":"",
                        "Address2":"",
                        "City":"",
                        "StateProvince":null,
                        "PostalCode":"",
                        "CountryCode":null
                    }
                },
                "ContactName":{
                    "FirstName":"",
                    "MiddleName":"",
                    "LastName":""
                }
            }
        }
    },
    "Company":{
        "Party":{
            "OrgInfo":{
                "CompanyName":"SF",
                "IDInfo":null
            },
            "ContactInfo":null
        }
    },
    "Facility":{
        "Party":{
            "OrgInfo":[

            ],
            "ContactInfo":{
                "ContactJobTitle":"",
                "Communications":[
                    {
                        "CommQualifier":null,
                        "CommPhone":"",
                        "CommEmail":"",
                        "Address":{
                            "Address1":"",
                            "Address2":"",
                            "City":"",
                            "StateProvince":null,
                            "PostalCode":"",
                            "CountryCode":null
                        }
                    },
                    null,
                    null,
                    null
                ],
                "ContactName":{
                    "FirstName":"Bob",
                    "MiddleName":"",
                    "LastName":"Tester"
                }
            }
        }
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-05-06 07:31:04

要求:递归扫描嵌套数组,剪除空的分支/项。

这是另一种“树上漫步”。

唯一“棘手”的部分是让树上更高的处理知道是否将当前项添加到输出树中。

处理节点的函数将返回一个数组,该数组具有'keep value‘标志以及要保留的值。

JSON Converted to array: Full Working code at eval.in

Output preserves original JSON Data Type: Full Working code at eval.in

代码:

function processList(array $list)
{
    $listResult = ['keepValue' => false, // once set true will propagate upward
                    'value'       => []];

    foreach ($list as $name => $item ) {
        if (is_null($item)) { // see is_scalar test
            continue;
        }

        if (is_scalar($item)) { // keep the value?
            if (!empty($item) || strlen(trim($item)) > 0) {
                $listResult['keepValue'] = true;
                $listResult['value'][$name] = $item;
            }

        } else { // new list... recurse

            $itemResult = processList($item);
            if ($itemResult['keepValue']) {
                $listResult['keepValue'] = true;
                $listResult['value'][$name] = $itemResult['value'];
            }
        }
    }   
    return $listResult;
}    

运行函数:

$source = json_decode($json, true);
$result = processList($source);
print_r($result['value']);

输出:

Array
(
    [Supplier] => Array
        (
            [Party] => Array
                (
                    [OrgInfo] => Array
                        (
                            [CompanyName] => Bobs
                            [IDInfo] => Array
                                (
                                    [1] => Array
                                        (
                                            [IDQualifierCode] => CompanyID
                                            [IDNum] => 83e26599-d40g-4cba-9791-7d7c83de282c
                                        )
                                    [2] => Array
                                        (
                                            [IDQualifierCode] => TID
                                            [IDNum] => BOBTID01020304
                                        )
                                    [3] => Array
                                        (
                                            [IDQualifierCode] => Token
                                            [IDNum] => c784570e-044d-42c8-98fe-af9f7c1747f5
                                        )
                                )
                        )
                )
        )
    [Company] => Array
        (
            [Party] => Array
                (
                    [OrgInfo] => Array
                        (
                            [CompanyName] => SF
                        )
                )
        )
    [Facility] => Array
        (
            [Party] => Array
                (
                    [ContactInfo] => Array
                        (
                            [ContactName] => Array
                                (
                                    [FirstName] => Bob
                                    [LastName] => Tester
                                )
                        )
                )
        )
)
票数 1
EN

Stack Overflow用户

发布于 2018-05-05 03:42:06

在经历了一些困惑之后,我想出了一个递归函数,就像我之前建议的那样,它将对象转换为一个数组,以检查变量是否设置为null。

如果该对象内的所有变量都为空,则对父对象进行索引以将该对象的引用设置为空。

我尽我所能地解释和记录代码。

请不要只是复制代码并完成它,而是通过阅读它,并尝试从我提供的代码中学习。

/**
    Unsets all empty variables in $object
*/
function loopTrough(&$object, &$parent = null, $key = null, $objectIsArray = false, $parentIsArray = false)
{
    // Keep track of amount of vars and amount of null vars
    $vars = 0;
    $null = 0;
    foreach($object as $index => $value)
    {
        $vars = $vars + 1;

        // Also check if is array
        $isArray = is_array($value);

        // Check if is object
        if (is_object($value) || $isArray) // using value here is save
        {
            // Loop trough the new object (or array) we found
            if ($objectIsArray)
            {
                loopTrough($object[$index], $object, $index, $isArray, $objectIsArray);
            }
            else
            {
                loopTrough($object->{$index}, $object, $index, $isArray, $objectIsArray);
            }
        }

        // Check if is null
        if ($objectIsArray)
        {
            if (!isset($object[$index]) || $object[$index] == ""){
                $null = $null + 1;
                // We don't want to show null
                unset($object[$index]);
            }
        }
        else
        {
            if (!isset($object->{$index}) || $object->{$index} == "") // use $object->{index} here, and not $value because $value does not change when object reference is changed
            {
                $null = $null + 1;
                // We don't want to show null
                unset($object->{$index});
            }
        }
    }

    // If there are as much null variables as variables
    if ($vars == $null && $parent !== null && $key !== null)
    {
        // Set the parent reference to null
        if ($parentIsArray) // Example exludes arrays, uncomment this if you want values in arrays to be recurisvely set to null
        {
            // unset($parent[$key]);
        }
        else
        {
            unset($parent->{$key});
        }   
    }
}



class Test
{
    public $one;

    public $two;
}

$test = new Test();

$test->one = new Test();
$test->one->two = "On";
$test->two = new Test();
$test->two->one = new Test(); 

var_dump($test);

loopTrough($test);

var_dump($test);
票数 1
EN

Stack Overflow用户

发布于 2018-05-05 02:59:37

正如我之前所建议的,您可以将object转换为array,并递归地循环遍历元素,以检查它们是否为null

递归函数将非常复杂,因为您不仅要删除null变量,而且要删除所有包含null的变量。

下面是一个没有递归的示例,它只是从object中删除null变量

class A
{
    public $var = "aVar";
    public $var1 = null;
}

$a = new A();

var_dump($a);

$array = (array)$a;

foreach($array as $key => $value)
{
    if (is_null($value)){
        unset($array[$key]);
    }
}

var_dump($array);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50180118

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档