开发者

parsing out the last number of the 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


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


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


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
                )

        )

)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜