]> git.mxchange.org Git - friendica.git/blob - src/Util/Arrays.php
Merge pull request #11519 from MrPetovan/task/11511-console-domain-move
[friendica.git] / src / Util / Arrays.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Util;
23
24 /**
25  * Array utility class
26  */
27 class Arrays
28 {
29         /**
30          * Private constructor
31          */
32         private function __construct() {
33                 // Utitlities don't have instances
34         }
35
36         /**
37          * Implodes recursively a multi-dimensional array where a normal implode() will fail.
38          *
39          * @param array  $array Array to implode
40          * @param string $glue  Glue for imploded elements
41          * @return string String with elements from array
42          */
43         public static function recursiveImplode(array $array, $glue) {
44                 // Init returned string
45                 $string = '';
46
47                 // Loop through all records
48                 foreach ($array as $element) {
49                         // Is an array found?
50                         if (is_array($element)) {
51                                 // Invoke cursively
52                                 $string .= '{' . self::recursiveImplode($element, $glue) . '}' . $glue;
53                         } else {
54                                 // Append normally
55                                 $string .= $element . $glue;
56                         }
57                 }
58
59                 // Remove last glue
60                 $string = trim($string, $glue);
61
62                 // Return it
63                 return $string;
64         }
65
66         /**
67          * walks recursively through an array with the possibility to change value and key
68          *
69          * @param array    $array    The array to walk through
70          * @param callable $callback The callback function
71          *
72          * @return array the transformed array
73          */
74         public static function walkRecursive(array &$array, callable $callback)
75         {
76                 $new_array = [];
77
78                 foreach ($array as $k => $v) {
79                         if (is_array($v)) {
80                                 if ($callback($v, $k)) {
81                                         $new_array[$k] = self::walkRecursive($v, $callback);
82                                 }
83                         } else {
84                                 if ($callback($v, $k)) {
85                                         $new_array[$k] = $v;
86                                 }
87                         }
88                 }
89                 $array = $new_array;
90
91                 return $array;
92         }
93 }