this is my first time on stackoverflow so sorry if I do something wrong. I would also appreciate your advice.
I have the next Array:
$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
]
]
And I want delete the null values. With array_filter also delete values 0, but I need those values 0. I tried with the following method:
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;
}
But, only delete the first value.
$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
]
]
And I would need the following array:
$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"
]
]
Can you help me please?. Thanks.
CodePudding user response:
That code does not seem to delete the 0
valued entries, but you do need to pass the parameter by reference if you want to see the changes in the calling process
$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));
RESULTS
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
(
)
)
CodePudding user response:
You should passing argument by reference.
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;
}
CodePudding user response:
Array filters remove null elements, so map your array using mapWithKeys, if each property is an array, use array_filter()
. Run a secondary filter, to remove the empty array.
$collection = collect($dataPayment);
$result = $collection->mapWithKeys(function ($item, $key) {
if (is_array($item)) {
$item = array_filter($item);
}
return [$key => $item];
})->filter()->all();
This should produce the expected results. If any problems with the code, please write.
CodePudding user response:
You are not storing the return from your recursive calls.
Try:
<?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));
Output:
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 ( ) )
CodePudding user response:
Your original array appears wrongly formatted. So I have help you reformat it into a proper PHP multi-dimensional array. You should be able to iterate over this without any problem.
$dataPayment = [
"terminal" => 12345,
"operationType" => null,
"payment" => array(
"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(
"startDate" => null,
"endDate" => null,
),
]