【发布时间】:2022-01-08 19:30:15
【问题描述】:
这是我第一次使用 stackoverflow,如果我做错了什么,非常抱歉。我也很感激你的建议。
我有下一个数组:
$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"
]
]
你能帮帮我吗?谢谢。
【问题讨论】:
标签: php arrays laravel null unset