Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added array_filter_recursive function

  • Loading branch information...
commit fb9920da8d81ae0e9555883b8fc1732f2f3cdefb 1 parent 189402d
unknown authored
Showing with 35 additions and 0 deletions.
  1. +35 −0 includes/func.array.php
View
35 includes/func.array.php
@@ -368,3 +368,38 @@ function array_compare($array1, $array2) {
return $diff;
}
+
+
+/** function array_filter_recursive
+ *
+ * Exactly the same as array_filter except this function
+ * filters within multi-dimensional arrays
+ *
+ * @param array
+ * @param string optional callback function name
+ * @param bool optional flag removal of empty arrays after filtering
+ * @return array merged array
+ */
+function array_filter_recursive($array, $callback = null, $remove_empty_arrays = false) {
+ foreach ($array as $key => & $value) { // mind the reference
+ if (is_array($value)) {
+ $value = array_filter_recursive($value, $callback);
+
+ if ($remove_empty_arrays && ! (bool) $value) {
+ unset($array[$key]);
+ }
+ }
+ else {
+ if ( ! is_null($callback) && ! $callback($value)) {
+ unset($array[$key]);
+ }
+ elseif ( ! (bool) $value) {
+ unset($array[$key]);
+ }
+ }
+ }
+ unset($value); // kill the reference
+
+ return $array;
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.