]> _ Git - cubist_util.git/commitdiff
wip #7904 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 18 Dec 2025 10:50:14 +0000 (11:50 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 18 Dec 2025 10:50:14 +0000 (11:50 +0100)
src/ArrayUtil.php

index 51db0ed7c0f3f09725b1bb51dabe28d0162e8609..8a311ccdd7b003238e39e1c89d3b76f22a76e144 100644 (file)
@@ -165,16 +165,42 @@ class ArrayUtil
     public static function merge()
     {
         $args = func_get_args();
-        $res = array();
+        $res = [];
         foreach ($args as $arg) {
             if (!is_array($arg)) {
-                $arg = array($arg);
+                $arg = [$arg];
             }
             $res = array_merge($res, $arg);
         }
         return $res;
     }
 
+    public static function mergeOverwriteNull()
+    {
+        $args = func_get_args();
+        foreach ($args as $k => $arg) {
+            self::cleanNullValues($arg);
+            $args[$k] = $arg;
+        }
+        return call_user_func_array('self::merge', $args);
+    }
+
+    public static function cleanNullValues(&$arr)
+    {
+        foreach ($arr as $k => $v) {
+            if (is_array($v)) {
+                self::cleanNullValues($v);
+                if (!count($v)) {
+                    unset($arr[$k]);
+                } else {
+                    $arr[$k] = $v;
+                }
+            } else if (null === $v) {
+                unset($arr[$k]);
+            }
+        }
+    }
+
     /**
      * @param $str
      * @return int[]