1

我有这个扣子:

foreach($jsonU as $j) {
        $jsonUId = $j->id;
        $jsonUName = $j->name;
        $jsonUDescription = $j->description;
        $jsonUDate = $j->date;
        $jsonUStatus = $j->status;
        $jsonUPicture = $j->picture;

        $jsonUncompleted[] = array('id'=> $jsonUId, 'name'=> $jsonUName, 'description' => $jsonUDescription, 'date' => $jsonUDate, 'status' => $jsonUStatus, 'picture' =>$jsonUPicture);
}  

只有当有值时,我才需要在数组中插入一个键。例如,$jsonUPictore并不总是有一个值,在这种情况下我不需要写那个键。

一些帮助?

4

2 回答 2

4

您可以使用array_filter带或不带参数的函数:

http://www.php.net/manual/en/function.array-filter.php

例子:

$jsonUncompleted[] = array_filter( array(
  'id'=> $jsonUId, 'name'=> $jsonUName, 'description' => $jsonUDescription,
  'date' => $jsonUDate, 'status' => $jsonUStatus, 'picture' =>$jsonUPicture
));
于 2012-06-29T16:44:34.833 回答
0

如果您想NULL从数组中删除所有值,有一种简单的方法可以使用array_filter最后一张海报提到的函数:

$new_array_without_nulls = array_filter($array_with_nulls, 'strlen');

来源:http ://briancray.com/posts/remove-null-values-php-arrays/

于 2012-06-29T16:48:29.807 回答