Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any PHP function that will strip properties of an object that are null?

Tags:

json

oop

php

I am returning a json_encode() of an array of objects pulled from an ORM. It includes lots of properties with a null value. What is the neatest way to remove these properties that are null? I guess I could iterate over the properties, look if they are null and then unset() that property, but surely there must be a more elegant way?

like image 650
Darren Avatar asked Dec 04 '10 06:12

Darren


People also ask

How do you remove null values from an object?

To remove all null values from an object: Use the Object. keys() method to get an array of the object's keys. Use the forEach() method to iterate over the array of keys. Check if each value is equal to null and delete the null values using the delete operator.

How do I remove a property from a PHP object?

You can delete an object's property using unset. unset($object->property_name);


4 Answers

Try this; it will only work on a simple object, but if it's coming from an ORM it should be simple enough.

// Strips any false-y values $object = (object) array_filter((array) $object); 

Thanks to Gordon's answer to another question yesterday for giving me the idea.

This works by

  • converting the object to an associative array, where object properties are the keys and their values are the array values
  • using array_filter with default arguments to remove array entries with a false (e.g. empty, or null) values
  • converting the new array back to a simple object

Note that this will remove all properties with empty values, including empty strings, false boolean values and 0s, not just nulls; you can change the array_filter call if you want to keep those and only remote those that are exactly null.

// Strips only null values $object = (object) array_filter((array) $object, function ($val) {     return !is_null($val); }); 
like image 162
El Yobo Avatar answered Oct 26 '22 13:10

El Yobo


I'm going to add to the response given by El Yobo because that will only work if you have a 1 dimensional object or array. If there is any array or object nesting then in order to get the accepted solution to work you must create some sort of recursive array filter. Not good.

The best solution my colleague and I came up with was to actually perform a regular expression on the JSON string before it was returned from the server.

$json = json_encode($complexObject); echo preg_replace('/,\s*"[^"]+":null|"[^"]+":null,?/', '', $json); 

The regular expression will remove all places in the string of the form ,"key":null including any whitespace between the leading comma and the start of the key. It will also match "key":null, afterwards to make sure that no null values were found at the beginning of a JSON object.

This obviously isn't the most ideal situation but it will effectively remove null entries without having to develop some kind of recursive array filter.

like image 21
Jim S. Avatar answered Oct 26 '22 12:10

Jim S.


Despite the name you can also use array_walk with a closure:

// Setup
$obj = (object) array('foo' => NULL, 'bar' => 'baz');

// equivalent to array_filter
array_walk($obj, function($v,$k) use ($obj) {
    if(empty($v)) unset($obj->$k);
});

// output
print_r($obj);

Output

stdClass Object
(
    [foo] => bar
)
like image 35
Gordon Avatar answered Oct 26 '22 11:10

Gordon


There's no standard function to remove null-valued properties. Writing one of your own isn't inelegant, if you write one elegantly.

like image 31
outis Avatar answered Oct 26 '22 11:10

outis