0 レビュー
4 回答
未設定の値null配列多次元php
<脇>
答えへのリンク
答えへのリンク
答えへのリンク
答えへのリンク
この質問にはすでに回答があります:
わからない
0
レビュー
答え :
解決策:
そのコードは0値のエントリを削除していないようですが、必要に応じて参照によってパラメータを渡す必要があります呼び出しプロセスの変更を確認する
$ Payment = [
"operationType" => null、
「ターミナル」=>12345、
"支払い"=>[
「ターミナル」=>12345、
"注文"=>"1234519997"、
"金額"=>1 0 0、
"通貨"=>"EUR"、
"セキュア"=>{-コード-1}、
"idUser" => 123456789、
"tokenUser" => "zidkeKeu68Kld"、
"urlOk" => null、
"urlKo" => null、
"originalIp" => "1.13 0 .151.28"、
"methodId" => 1、
"trxType" => "N"、
"userInteraction" => 1、
"scaException" => "MIT"
]、
"サブスクリプション"=>[
"startDate" => null、
"endDate" => null
]
];
クラスxxx
{{
プライベート関数arrayUnset(&$ dataPayment)
{{
foreach($ dataPayment as $ key => $ value){
if(is_array($ dataPayment [$ key])){
$ this-> arrayUnset($ dataPayment [$ key]);
}
if($ value === null || $ value === ""){
unset($ dataPayment [$ key]);
}
}
$dataPaymentを返します。
}
パブリック関数zzz($ data)
{{
$ this-> arrayUnset($ data);を返します。
}
}
$ obj = new xxx;
print_r($ obj-> zzz($ Payment));
結果
配列
((
[ターミナル]=>12345
[支払い]=>配列
((
[ターミナル]=>12345
[注文]=>1234519997
[金額]=>1 0 0
[通貨]=>EUR
[安全]=>{-コード-1}
[idUser] => 123456789
[tokenUser] => zidkeKeu68Kld
[originalIp] => 1.13 0 .151.28
[methodId] => 1
[trxType] => N
[userInteraction] => 1
[scaException] => MIT
)。
[サブスクリプション]=>配列
((
)。
)。
わからない
0
レビュー
答え :
解決策:
参照によって引数を渡す必要があります。
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
レビュー
答え :
解決策:
配列フィルターはnull要素を削除するため、mapWithKeysを使用して配列をマップします。各プロパティが配列の場合は、 array_filter()
を使用します。 2次フィルターを実行して、空のアレイを削除します。
$collection = collect($dataPayment);
$result = $collection->mapWithKeys(function ($item, $key) {
if (is_array($item)) {
$item = array_filter($item);
}
return [$key => $item];
})->filter()->all();
これにより、期待どおりの結果が得られるはずです。コードに問題がある場合は、書いてください。
わからない
0
レビュー
答え :
解決策:
再帰呼び出しからのリターンを保存していません。
試してみてください:
<?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));
出力:
配列
((
[ターミナル]=>12345
[支払い]=>配列
((
[ターミナル]=>12345
[注文]=>1234519997
[金額]=>100
[通貨]=>EUR
[安全]=>0
[idUser] => 123456789
[tokenUser] => zidkeKeu68Kld
[originalIp] => 1.130.151.28
[methodId] => 1
[trxType] => N
[userInteraction] => 1
[scaException] => MIT
)。
[サブスクリプション]=>配列
((
)。
)。
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。