I've this bucle:
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);
}
I need to insert a key in the array only if have value. For example, $jsonUPictore
not always has a value and in that case I don't need to write that key.
Some help?
You could use the array_filter
function with or without a parameter:
http://www.php.net/manual/en/function.array-filter.php
Example:
$jsonUncompleted[] = array_filter( array(
'id'=> $jsonUId, 'name'=> $jsonUName, 'description' => $jsonUDescription,
'date' => $jsonUDate, 'status' => $jsonUStatus, 'picture' =>$jsonUPicture
));
If you are wanting to remove all NULL
values from an array there's an easy way to do it using the array_filter
function the last poster mentioned:
$new_array_without_nulls = array_filter($array_with_nulls, 'strlen');
Source: http://briancray.com/posts/remove-null-values-php-arrays/