2 回答
TA贡献1848条经验 获得超2个赞
这是一种方法:
<?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);
我个人不喜欢这样,因为您将不同类型的值传递给过滤函数。对于强类型语言,这是不允许的。但另一种选择是列举“好的”属性,因此在这种情况下这似乎是一个很好的折衷方案。
TA贡献1900条经验 获得超5个赞
您不需要循环,只需使用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 关注
- 95 浏览
添加回答
举报