解析出帖子的最后一个数字

时间:2011-04-28 18:33:35

标签: php regex parsing post

好的,所以我有一篇看起来像这样的帖子

[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
 )

我如何获得最高数字。例如,可选的“optional_premium_ ”最高为3,“premium _ ”可选最高为4.我如何找到此$ _POST中的最高

4 个答案:

答案 0 :(得分:2)

在我回答之前,我有2个问题有1个问题,这就是为什么你使用嵌入式阵列?如果您使用标准符号,那么您的帖子会更简单:

$_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);
}

//然后再次循环以比较每一个

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

应该让你获得最高分,但是你必须回过头来比较它们以做你最终的计划。

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

答案 1 :(得分:2)

您可以使用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);

答案 2 :(得分:2)

遍历所有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
                )

        )

)

答案 3 :(得分:1)

虽然无效,但您可以尝试类似

的内容
$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"]进行搜索。