我是这个小伙子:
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
并不总是有值,在这种情况下,我不需要写那个密钥。
一些帮助?
答案 0 :(得分: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
));
答案 1 :(得分:0)
如果您想要从数组中删除所有NULL
值,可以使用上面提到的最后一张海报的array_filter
函数轻松完成此操作:
$new_array_without_nulls = array_filter($array_with_nulls, 'strlen');
来源:http://briancray.com/posts/remove-null-values-php-arrays/