取消设置值空数组多维 php

unset values null array multidimensional php

这是我第一次使用 Whosebug,如果我做错了什么,请见谅。我也非常感谢您的建议。

我有下一个数组:

$dataPayment= [
  "operationType" => null
  "terminal" => 12345
  "payment" => array:14 [
    "terminal" => 12345
    "order" => "1234519997"
    "amount" => 100
    "currency" => "EUR"
    "secure" => 0
    "idUser" => 123456789"
    "tokenUser" => "zidkeKeu68Kld"
    "urlOk" => null
    "urlKo" => null
    "originalIp" => "1.130.151.28"
    "methodId" => 1
    "trxType" => "N"
    "userInteraction" => 1
    "scaException" => "MIT"
  ]
  "subscription" => array:2 [
    "startDate" => null
    "endDate" => null
  ]
]

我想删除空值。使用 array_filter 也删除值 0,但我需要这些值 0。我尝试使用以下方法:

private function arrayUnset( $dataPayment )
{
    foreach( $dataPayment as $key => $value ) 
    {
        if( is_array( $dataPayment[ $key ] ) )
        {
           $this->arrayUnset( $dataPayment[ $key ] );
        }
        if( $value === null || $value === "" ) 
        {
            unset( $dataPayment[ $key ] ); 
        } 
    }
    return $dataPayment;
}

但是,只删除第一个值。

$dataPayment = [
  "terminal" => 12345
  "payment" => array:14 [
    "terminal" => 12345
    "order" => "1234519997"
    "amount" => 100
    "currency" => "EUR"
    "secure" => 0
    "idUser" => 123456789"
    "tokenUser" => "zidkeKeu68Kld"
    "urlOk" => null
    "urlKo" => null
    "originalIp" => "1.130.151.28"
    "methodId" => 1
    "trxType" => "N"
    "userInteraction" => 1
    "scaException" => "MIT"
  ]
  "subscription" => array:2 [
    "startDate" => null
    "endDate" => null
  ]
]

我需要以下数组:

$dataPayment = [
      "terminal" => 12345
      "payment" => array:14 [
        "terminal" => 12345
        "order" => "1234519997"
        "amount" => 100
        "currency" => "EUR"
        "secure" => 0
        "idUser" => 123456789"
        "tokenUser" => "zidkeKeu68Kld"
        "originalIp" => "1.130.151.28"
        "methodId" => 1
        "trxType" => "N"
        "userInteraction" => 1
        "scaException" => "MIT"
      ]
    ]

你能帮帮我吗?谢谢。

您应该通过引用传递参数。

private function arrayUnset( &$dataPayment )
{
    foreach( $dataPayment as $key => $value ) 
    {
        if( is_array( $dataPayment[ $key ] ) )
        {
           $dataPayment[ $key ] = $this->arrayUnset($value);
        }
        if( $value === null || $value === "" ) 
        {
            unset( $dataPayment[ $key ] ); 
        } 
    }
    return $dataPayment;
}

该代码似乎没有删除 0 值的条目,但如果您想查看调用过程中的变化,您确实需要通过引用传递参数

$Payment = [
    "operationType" => null,
    "terminal" => 12345,
    "payment" => [
      "terminal" => 12345,
      "order" => "1234519997",
      "amount" => 100,
      "currency" => "EUR",
      "secure" => 0,
      "idUser" => 123456789,
      "tokenUser" => "zidkeKeu68Kld",
      "urlOk" => null,
      "urlKo" => null,
      "originalIp" => "1.130.151.28",
      "methodId" => 1,
      "trxType" => "N",
      "userInteraction" => 1,
      "scaException" => "MIT"
    ],
    "subscription" =>  [
        "startDate" => null,
        "endDate" => null
    ]
];
class xxx
{
    private function arrayUnset( &$dataPayment )
    {
        foreach( $dataPayment as $key => $value ) {
            if( is_array( $dataPayment[ $key ] ) ) {
               $this->arrayUnset( $dataPayment[ $key ] );
            }
            if( $value === null || $value === "" ) {
                unset( $dataPayment[ $key ] ); 
            } 
        }
        return $dataPayment;
    }

    public function zzz($data)
    {
        return $this->arrayUnset($data);
    }
}

$obj = new xxx;

print_r($obj->zzz($Payment));

结果

Array
(
    [terminal] => 12345
    [payment] => Array
        (
            [terminal] => 12345
            [order] => 1234519997
            [amount] => 100
            [currency] => EUR
            [secure] => 0
            [idUser] => 123456789
            [tokenUser] => zidkeKeu68Kld
            [originalIp] => 1.130.151.28
            [methodId] => 1
            [trxType] => N
            [userInteraction] => 1
            [scaException] => MIT
        )

    [subscription] => Array
        (
        )

)

数组过滤器删除空元素,因此使用 mapWithKeys 映射您的数组,如果每个 属性 都是一个数组,请使用 array_filter()。 运行 二次过滤,去除空数组。

$collection = collect($dataPayment);

$result = $collection->mapWithKeys(function ($item, $key) {

    if (is_array($item)) {
        $item = array_filter($item);
    }

    return [$key => $item];
})->filter()->all();

这应该会产生预期的结果。代码有问题请留言

您没有存储来自递归调用的 return。

尝试:

<?php

$Payment = [
    "operationType" => null,
    "terminal" => 12345,
    "payment" => [
      "terminal" => 12345,
      "order" => "1234519997",
      "amount" => 100,
      "currency" => "EUR",
      "secure" => 0,
      "idUser" => 123456789,
      "tokenUser" => "zidkeKeu68Kld",
      "urlOk" => null,
      "urlKo" => null,
      "originalIp" => "1.130.151.28",
      "methodId" => 1,
      "trxType" => "N",
      "userInteraction" => 1,
      "scaException" => "MIT"
    ],
    "subscription" =>  [
        "startDate" => null,
        "endDate" => null
    ]
];

function arrayUnset($dataPayment) {
    foreach($dataPayment as $key => $value) 
        if(is_array($dataPayment[$key]))
            $dataPayment[$key]=arrayUnset($dataPayment[$key]);
        else if ($value==null || $value=="") 
            unset($dataPayment[$key]); 
    return $dataPayment;
}

print_r(arrayUnset($Payment));

输出:

Array
(
    [terminal] => 12345
    [payment] => Array
        (
            [terminal] => 12345
            [order] => 1234519997
            [amount] => 100
            [currency] => EUR
            [secure] => 0
            [idUser] => 123456789
            [tokenUser] => zidkeKeu68Kld
            [originalIp] => 1.130.151.28
            [methodId] => 1
            [trxType] => N
            [userInteraction] => 1
            [scaException] => MIT
        )

    [subscription] => Array
        (
        )

)

Teh Playground!