$value) { if (call_user_func($closure, $value, $key)) { $result[] = $value; } } return $result; } return array_filter($array); } /** * Get a nested value inside an array. Dot notation is supported. * * @since 2.0.11 * * @param array $array The array to get the value from. * @param string $key The array key to get. Supports dot notation. * @param mixed $default The value to return ibn the case the key does not exist. * @return mixed */ public static function get($array, $key, $default = null) { if (is_null($key)) { return $array; } if (isset($array[ $key ])) { return $array[ $key ]; } foreach (explode('.', $key) as $segment) { if ( ! is_array($array) || ! array_key_exists($segment, $array)) { return $default; } $array = $array[ $segment ]; } return $array; } /** * Set a nested value inside an array. Dot notation is supported. * * @since 2.0.11 * * @param array $array The array to modify. * @param string $key The array key to set. Supports dot notation. * @param mixed $value The value to set. * @return array */ public static function set(&$array, $key, $value) { if (is_null($key)) { return $array = $value; // phpcs:ignore } $keys = explode('.', $key); while (count($keys) > 1) { $key = array_shift($keys); if ( ! isset($array[ $key ]) || ! is_array($array[ $key ])) { $array[ $key ] = []; } $array =& $array[ $key ]; } $array[ array_shift($keys) ] = $value; return $array; } /** * Static class only. * * @since 2.0.11 */ private function __construct() {} }