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