有条件地在PHP数组中显示值

时间:2018-11-21 14:47:33

标签: php arrays

我有一个PHP数组,该数组具有一些要存储到API的值,我想显示具有值的字段并省略其他值,但不能确定如何用PHP编写逻辑,

〜请协助?

array:5 [
  "child1Dob" => "2018-11-07",
  "child2Dob" => null,
  "child3Dob" => null,
  "child4Dob" => null,
  "child5Dob" => null,
]

2 个答案:

答案 0 :(得分:1)

您可以使用array_filter仅保留非Null的值。 尝试这样。

array_filter($array);

答案 1 :(得分:0)

尝试

$b = array_filter($a, function($k) { return $k!=null; });

工作示例here