File modules/util/ArrayUtils.class.php
Last commit: Sun Oct 13 15:00:13 2024 +0200 Jan Dankert Refactoring CSS: Using native CSS variables for theme colors.
1 <?php 2 3 namespace util; 4 /** 5 * Created by PhpStorm. 6 * User: dankert 7 * Date: 22.12.18 8 * Time: 21:36 9 */ 10 class ArrayUtils 11 { 12 13 public static function getSubArray($array, $keys) 14 { 15 16 $a = $array; 17 foreach ($keys as $k) { 18 if (!isset($a[$k])) 19 return array(); 20 21 if (!is_array($a[$k])) 22 return array(); 23 24 $a = $a[$k]; 25 } 26 27 return $a; 28 } 29 30 31 /** 32 * Dives into an array and gets a value with a sub key. 33 * @param array $array 34 * @param array $keys 35 * @return mixed|null 36 */ 37 public static function getSubValue($array, $keys) 38 { 39 40 $a = $array; 41 foreach ($keys as $k) { 42 if (!isset($a[$k])) 43 return null; 44 45 $a = $a[$k]; 46 } 47 48 return $a; 49 } 50 51 52 /** 53 * Maps an array to a new array. 54 * @param $callback callable callback, it is called with 3 parameters: the new array, key, value. 55 * @param $array array array which should be mapped 56 * @return array the new array 57 */ 58 public static function mapToNewArray($array, $callback) { 59 $newArray = []; 60 foreach( $array as $key => $value ) { 61 $newArray += (array) call_user_func( $callback, $key, $value ); 62 } 63 return $newArray; 64 } 65 66 67 68 /** 69 * Make a flat array. 70 * 71 * @param $arr 72 * @param $padChar 73 * @param int $depth 74 * @return array 75 */ 76 public static function flatArray($arr, $path = []) 77 { 78 $new = array(); 79 80 foreach ($arr as $key => $val) { 81 $newPath = array_merge($path,[$key]); 82 if (is_array($val)) { 83 $new[implode('.',$newPath)] = [ 84 'path' => $newPath, 85 'value'=> '' 86 ]; 87 $new += self::flatArray($val, $newPath); 88 } else 89 $new[implode('.',$newPath)] = [ 90 'path' => $newPath, 91 'value'=> $val 92 ]; 93 } 94 return $new; 95 } 96 97 98 /** 99 * Map keys. 100 * 101 * @param $callback 102 * @param $array 103 * @return mixed 104 */ 105 public static function mapKeys($callback, $array) 106 { 107 $newArray = []; 108 foreach( $array as $key=>$value ) { 109 $newArray[ call_user_func( $callback,$key)] = $value; 110 } 111 return $newArray; 112 } 113 }
Downloadmodules/util/ArrayUtils.class.php
History Sun, 13 Oct 2024 15:00:13 +0200 Jan Dankert Refactoring CSS: Using native CSS variables for theme colors. Tue, 14 May 2024 23:48:28 +0200 Jan Dankert Enabling Fulltext-Search in Settings Sun, 5 Dec 2021 22:09:08 +0100 dankert Fix: The Diff function was broken. Sun, 25 Oct 2020 01:45:39 +0200 Jan Dankert Fix: the flattened arrays were missing some values oO Sat, 22 Feb 2020 23:58:02 +0100 Jan Dankert Refactoring: Namespacing for module 'util'. Tue, 12 Nov 2019 22:49:28 +0100 Jan Dankert New: Nicer output of the whole configuration. Mon, 11 Nov 2019 00:43:47 +0100 Jan Dankert New: node settings could have config variables; macro settings could have settings variables. Sun, 10 Nov 2019 21:55:22 +0100 Jan Dankert New: Page templates have access to the settings of the node object. Sat, 22 Dec 2018 22:13:47 +0100 Jan Dankert Bei der Filterung nach dem entsprechenden Publish-Typ unterscheiden.