简体   繁体   中英

suppress key from an array search and get the next key

How to suppress a key from a multidimensional array and get the next occurrence key.

$array_example = array(

    'default_group' => 'styling',

    'general'   => esc_html__('General', THEMETEXTDOMAIN),

    // User can enter it here
    // 'default_group' => 'styling',

    'styling'   => esc_html__('Styling', THEMETEXTDOMAIN),

    // 'default_group' => 'styling',

    'animate'   => esc_html__('Animate', THEMETEXTDOMAIN),
)

[default_group] key is a fixed key but it might be at any position.

I need to get the next key if the [default_group] key not exist or it has an empty value.

if( array_key_exists( 'default_group', $array_example ) && !empty( $array_example['default_group'] ) ) {

     $col_group_default = sanitize_text_field( $array_example['default_group'] );

} else {

     // Here I want to get the key BUT NOT [default_group]                                

}
$choice = '';
$default = array_search('default_group', array_keys($array_example));

if ( $default && !empty($array_example[ $default ]) ) {
  $choice = $array_example[ $default ];
} else {
  $choice = isset($array_example[ $default + 1]) ? $array_example[ $default + 1] : $array_example[0];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM