如何从多维数组中抑制键并获取下一个出现键。
$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]键是固定键,但它可能位于任何位置。
如果[default_group]键不存在或者它的值为空,我需要获取下一个键。
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]
}
答案 0 :(得分:0)
$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];
}