解析帖子的最后一个数字

发布于 2024-11-04 03:10:16 字数 546 浏览 1 评论 0原文

好吧,我有一篇文章看起来有点像这样,

[optional_premium_1] => Array
    (
        [0] => 61
    )
[optional_premium_2] => Array
    (
        [0] => 55
    )
[optional_premium_3] => Array
    (
        [0] => 55
    )
[premium_1] => Array
  (
     [0] => 33
 )
[premium_2] => Array
  (
     [0] => 36     )
[premium_3] => Array
  (
     [0] => 88     )

[premium_4] => Array
  (
     [0] => 51
 )

我如何从中获得最高的数字。例如,可选的“可选_premium_”最高为 3,而“premium_”可选最高为 4。我如何找到此 $_POST 中的最高值

Ok so i have a post that looks kind of this

[optional_premium_1] => Array
    (
        [0] => 61
    )
[optional_premium_2] => Array
    (
        [0] => 55
    )
[optional_premium_3] => Array
    (
        [0] => 55
    )
[premium_1] => Array
  (
     [0] => 33
 )
[premium_2] => Array
  (
     [0] => 36     )
[premium_3] => Array
  (
     [0] => 88     )

[premium_4] => Array
  (
     [0] => 51
 )

how do i get the highest number out of the that. So for example, the optional "optional_premium_" highest is 3 and the "premium_" optional the highest is 4. How do i find the highest in this $_POST

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓬勃野心 2024-11-11 03:10:16

您可以使用 array_key_exists(),也许是这样的:

function getHighest($variableNamePrefix, array $arrayToCheck) {
    $continue = true;
    $highest = 0;
    while($continue) {
        if (!array_key_exists($variableNamePrefix . "_" . ($highest + 1) , $arrayToCheck)) {
            $continue = false;
        } else {
           highest++;
        }
    }
    //If 0 is returned than nothing was set for $variableNamePrefix 
    return $highest;
}

$highestOptionalPremium = getHighest('optional_premium', $_POST);
$highestPremium = getHighest('premium', $_POST);

You could use array_key_exists(), perhaps something like this:

function getHighest($variableNamePrefix, array $arrayToCheck) {
    $continue = true;
    $highest = 0;
    while($continue) {
        if (!array_key_exists($variableNamePrefix . "_" . ($highest + 1) , $arrayToCheck)) {
            $continue = false;
        } else {
           highest++;
        }
    }
    //If 0 is returned than nothing was set for $variableNamePrefix 
    return $highest;
}

$highestOptionalPremium = getHighest('optional_premium', $_POST);
$highestPremium = getHighest('premium', $_POST);
歌入人心 2024-11-11 03:10:16

在回答之前,我有 1 个问题,分为 2 个部分,这就是为什么你要使用嵌入式数组?如果您使用以下标准符号,您的文章会简单得多:

$_POST['form_input_name'] = 'whatever';

除非您出于某种原因专门使用数组构建这篇文章。这样你就可以正常使用数组键作为变量名和数组值。

因此:

$arr = array(
"optional_premium_1" => "61"
"optional_premium_2" => "55"
);

您可以使用

$key = array_keys($arr);
//gets the keys for that array
//then loop through get raw values

foreach($key as $val){
  str_replace("optional_premium_", '', $val);
}

//then 再次循环来比较每个

$highest = 0;
for each($key as $val){
  if ((int)$val > $highest) $highest = (int)$val;
 }

应该为您提供最高值的值,但随后您必须返回并比较它们以执行您的最终计划。

您还可以将它们分成 2 个单独的数组,并假设它们是按顺序添加的,只需使用 end() http://php.net/manual/en/function.end.php

I have 1 question with 2 parts before I answer, and that is why are you using embedded arrays? Your post would be much simpler if you used a standard notation like:

$_POST['form_input_name'] = 'whatever';

unless you are specifically building this post with arrays for some reason. That way you could use the array key as the variable name and the array value normally.

So given:

$arr = array(
"optional_premium_1" => "61"
"optional_premium_2" => "55"
);

you could use

$key = array_keys($arr);
//gets the keys for that array
//then loop through get raw values

foreach($key as $val){
  str_replace("optional_premium_", '', $val);
}

//then loop through again to compare each one

$highest = 0;
for each($key as $val){
  if ((int)$val > $highest) $highest = (int)$val;
 }

that should get you the highest one, but then you have to go back and compare them to do whatever your end plan for it was.

You could also break those into 2 separate arrays and assuming they are added in order just use end() http://php.net/manual/en/function.end.php

顾冷 2024-11-11 03:10:16

循环遍历所有 POST 数组元素,挑选出键名称与“name_number”模式匹配的元素,并保存键名称中数字部分最大的元素。这是执行此操作的 PHP 脚本:

<?php // test.php 20110428_0900

// Build temporary array to simulate $_POST
$TEMP_POST = array(
    "optional_premium_1"    => array(61),
    "optional_premium_2"    => array(55),
    "optional_premium_3"    => array(55),
    "premium_1"             => array(33),
    "premium_2"             => array(36),
    "premium_3"             => array(88),
    "premium_4"             => array(51),
);

$names = array();       // Array of POST variable names
// loop through all POST array elements
foreach ($TEMP_POST as $k => $v) {
    // Process only elements with names matching "word_number" pattern.
    if (preg_match('/^(\w+)_(\d+)$/', $k, $m)) {
        $name   = $m[1];
        $number = (int)$m[2];
        if (!isset($names[$name]))
        {   // Add new POST var key name to names array
            $names[$name] = array(
                "name"      => $name,
                "max_num"   => $number,
                "key_name"  => $k,
                "value"     => $v,
                );
        } elseif ($number > $names[$name]['max_num'])
        { // New largest number in key name.
            $names[$name] = array(
                "name"      => $name,
                "max_num"   => $number,
                "key_name"  => $k,
                "value"     => $v,
                );
        }
    }
}
print_r($names);
?>

这是脚本的输出:

Array
(
    [optional_premium] => Array
        (
            [name] => optional_premium
            [max_num] => 3
            [key_name] => optional_premium_3
            [value] => Array
                (
                    [0] => 55
                )

        )

    [premium] => Array
        (
            [name] => premium
            [max_num] => 4
            [key_name] => premium_4
            [value] => Array
                (
                    [0] => 51
                )

        )

)

Loop through all POST array elements, pick out elements having key names matching "name_number" pattern and save the ones having the largest number portion of the key names. Here is a PHP script which does it:

<?php // test.php 20110428_0900

// Build temporary array to simulate $_POST
$TEMP_POST = array(
    "optional_premium_1"    => array(61),
    "optional_premium_2"    => array(55),
    "optional_premium_3"    => array(55),
    "premium_1"             => array(33),
    "premium_2"             => array(36),
    "premium_3"             => array(88),
    "premium_4"             => array(51),
);

$names = array();       // Array of POST variable names
// loop through all POST array elements
foreach ($TEMP_POST as $k => $v) {
    // Process only elements with names matching "word_number" pattern.
    if (preg_match('/^(\w+)_(\d+)$/', $k, $m)) {
        $name   = $m[1];
        $number = (int)$m[2];
        if (!isset($names[$name]))
        {   // Add new POST var key name to names array
            $names[$name] = array(
                "name"      => $name,
                "max_num"   => $number,
                "key_name"  => $k,
                "value"     => $v,
                );
        } elseif ($number > $names[$name]['max_num'])
        { // New largest number in key name.
            $names[$name] = array(
                "name"      => $name,
                "max_num"   => $number,
                "key_name"  => $k,
                "value"     => $v,
                );
        }
    }
}
print_r($names);
?>

Here is the output from the script:

Array
(
    [optional_premium] => Array
        (
            [name] => optional_premium
            [max_num] => 3
            [key_name] => optional_premium_3
            [value] => Array
                (
                    [0] => 55
                )

        )

    [premium] => Array
        (
            [name] => premium
            [max_num] => 4
            [key_name] => premium_4
            [value] => Array
                (
                    [0] => 51
                )

        )

)
傲娇萝莉攻 2024-11-11 03:10:16

虽然效果不佳,但您可以尝试类似的方法。

$largest = 0;

foreach($_POST as $key => $value)
{
    $len = strlen("optional_premium_");
    $num = substr($key, $len);

    if($num > $largest)
        $largest = $num;
}

print_r($largest);

问题是这仅适用于一组类别。它很可能会在整个脚本中给出错误。

理想情况下,您需要重新组织您的帖子,使每个数组类似于

[optional_premium_1] => Array
(
    [0] => 61
    ["key"] => 1
)
[optional_premium_2] => Array
(
    [0] => 61
    ["key"] => 2
)

然后只需 foreach 并使用 $array["key"] 进行搜索。

Though ineffective, you could try something like

$largest = 0;

foreach($_POST as $key => $value)
{
    $len = strlen("optional_premium_");
    $num = substr($key, $len);

    if($num > $largest)
        $largest = $num;
}

print_r($largest);

The problem being that this will only work for one set of categories. It will most likely give errors throughout the script.

Ideally, you would want to reorganize your post, make each array be something like

[optional_premium_1] => Array
(
    [0] => 61
    ["key"] => 1
)
[optional_premium_2] => Array
(
    [0] => 61
    ["key"] => 2
)

Then just foreach and use $array["key"] to search.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文