2 回答

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊
這是一種方法:
<?php
$s = <<<eof
[{"ticketNumber": "Ha&51","qty": 0,"title": "Wheat", "price":
44.2,"purchaseDate": null,"date": null},{ "ticketNumber": "H88i51","qty":
2, "title": "Prince", "price": 12.99,"purchaseDate": null,"date": null}]
eof;
$m_json = json_decode($s, true);
$f = fn ($v): bool => $v !== null;
foreach ($m_json as $m_tick) {
$a[] = array_filter($m_tick, $f);
}
var_export($a);
我個(gè)人不喜歡這樣,因?yàn)槟鷮⒉煌愋偷闹祩鬟f給過(guò)濾函數(shù)。對(duì)于強(qiáng)類型語(yǔ)言,這是不允許的。但另一種選擇是列舉“好的”屬性,因此在這種情況下這似乎是一個(gè)很好的折衷方案。

TA貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個(gè)贊
您不需要循環(huán),只需使用array_filter
<?php
$array = array(1 => "PHP code tester Sandbox Online",? 2 => null);
print_r($array);
$arrayFiltered = array_filter($array);
print_r($arrayFiltered);
- 2 回答
- 0 關(guān)注
- 186 瀏覽
添加回答
舉報(bào)