从字符串中获取最大值

Getting the maximum value from strings

我在 WordPress 中有如下选项字符串:

$option[max_slider_function_1];
$option[max_slider_function_2];
$option[max_slider_function_3];
$option[max_slider_function_4];
$option[max_slider_function_5];

...............................

$option[max_slider_function_15];

我想要最大值,在本例中是 15。

备注

字符串$option[max_slider_function_<number>]可以扩展到很长的数字。

有什么帮助吗?

排序数组 (usort()) 并回调以先对数组进行相应排序,然后获取最后一个值 (end())。

function compare_value($a, $b)
{
    $a = end(explode('_', $a));
    $b = end(explode('_', $b));
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}

usort($option , "compare_value");
$max = end($option);

Working Demo

使用 php 函数到 ksort 数组键然后使用结束函数获取最后一个元素

ksort($option);
echo end($option);