X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FUtil%2FArrays.php;h=ae1b1b73ef34e971601b9ace2799c0850400518e;hb=da58b894a4239e95342524eeacb85af7bf6c5a9d;hp=e2ba412ca66bde7ea3c1e63e86c3adc0546cc972;hpb=dcbd44ab883d68872b939182b3070a47afa58f40;p=friendica.git diff --git a/src/Util/Arrays.php b/src/Util/Arrays.php index e2ba412ca6..ae1b1b73ef 100644 --- a/src/Util/Arrays.php +++ b/src/Util/Arrays.php @@ -1,8 +1,24 @@ + * @copyright Copyright (C) 2010-2023, the Friendica project + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * */ + namespace Friendica\Util; /** @@ -13,7 +29,7 @@ class Arrays /** * Private constructor */ - private function __construct () { + private function __construct() { // Utitlities don't have instances } @@ -24,7 +40,7 @@ class Arrays * @param string $glue Glue for imploded elements * @return string String with elements from array */ - public static function recursiveImplode (array $array, $glue) { + public static function recursiveImplode(array $array, $glue) { // Init returned string $string = ''; @@ -46,4 +62,32 @@ class Arrays // Return it return $string; } + + /** + * walks recursively through an array with the possibility to change value and key + * + * @param array $array The array to walk through + * @param callable $callback The callback function + * + * @return array the transformed array + */ + public static function walkRecursive(array &$array, callable $callback) + { + $new_array = []; + + foreach ($array as $k => $v) { + if (is_array($v)) { + if ($callback($v, $k)) { + $new_array[$k] = self::walkRecursive($v, $callback); + } + } else { + if ($callback($v, $k)) { + $new_array[$k] = $v; + } + } + } + $array = $new_array; + + return $array; + } }