Commit 137b7f48 authored by dbourdere's avatar dbourdere 🤠

use array filter

parent fd9eeac3
......@@ -34,24 +34,26 @@ class JsonSerializer
*/
public static function stringify($object)
{
$object = self::getInstance()->serialize($object, "json");
$array = json_decode($object, true);
self::removeEmptyValuesAndSubarrays($array);
$object = json_encode($array);
$array = self::getInstance()->normalize($object);
$arrayfiltered = self::array_filter_recursive($array, 'self::is_notnull');
$object = json_encode($arrayfiltered);
return $object;
}
public static function removeEmptyValuesAndSubarrays(&$array){
foreach($array as $k=>&$v){
if(is_array($v)){
$v=self::removeEmptyValuesAndSubarrays($v);
if(!count($v)){
unset($array[$k]);
}
}elseif(!strlen($v)){
unset($array[$k]);
public static function array_filter_recursive($input, $callback = null)
{
foreach ($input as &$value)
{
if (is_array($value))
{
$value = self::array_filter_recursive($value, $callback);
}
}
return $array;
return array_filter($input, $callback);
}
public static function is_notnull($v) {
return !is_null($v);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment