Make sp_array_value map recursively for multidimensional arrays

This commit is contained in:
Brian Miyaji
2021-11-05 23:56:41 +09:00
parent f7ed06dddc
commit bf0e91919a

View File

@@ -305,6 +305,15 @@ if ( !function_exists( 'sp_array_between' ) ) {
} }
} }
if ( !function_exists( 'sp_array_map_recursive' ) ) {
function sp_array_map_recursive( callable $func, array $arr ) {
array_walk_recursive( $arr, function( &$v ) use ( $func ) {
$v = $func( $v );
} );
return $arr;
}
}
if ( !function_exists( 'sp_array_value' ) ) { if ( !function_exists( 'sp_array_value' ) ) {
function sp_array_value( $arr = array(), $key = 0, $default = null, $sanitize = false ) { function sp_array_value( $arr = array(), $key = 0, $default = null, $sanitize = false ) {
$value = ( isset( $arr[ $key ] ) ? $arr[ $key ] : $default ); $value = ( isset( $arr[ $key ] ) ? $arr[ $key ] : $default );
@@ -313,16 +322,16 @@ if ( !function_exists( 'sp_array_value' ) ) {
if ( is_array( $value ) ): if ( is_array( $value ) ):
switch ( $sanitize ): switch ( $sanitize ):
case 'int': case 'int':
$value = array_map( 'intval', $value ); $value = sp_array_map_recursive( 'intval', $value );
break; break;
case 'title': case 'title':
$value = array_map( 'sanitize_title', $value ); $value = sp_array_map_recursive( 'sanitize_title', $value );
break; break;
case 'text': case 'text':
$value = array_map( 'sanitize_text_field', $value ); $value = sp_array_map_recursive( 'sanitize_text_field', $value );
break; break;
case 'key': case 'key':
$value = array_map( 'sanitize_key', $value ); $value = sp_array_map_recursive( 'sanitize_key', $value );
break; break;
endswitch; endswitch;
else: else: