开发者

Trying to parse out the $_POST key names that I don't want

开发者 https://www.devze.com 2023-02-02 07:32 出处:网络
$post_keys = array_keys($_POST); $special_keys = array(); f开发者_如何学运维or($i=0;$i<count($post_keys);$i++){
$post_keys = array_keys($_POST);
$special_keys = array();

f开发者_如何学运维or($i=0;$i<count($post_keys);$i++){
    if(strpos($post_keys[$i], "special") !== false){

        $special_keys[] = $post_keys[$i];

    }
}

I have numerous post vars with naming conventions such as special0, special0_time, special0_duration, special1, special1_time etc.... What I want to do is to find out how many main groups there are, so special0, special1 etc... The code I currently have searches all key names that have "special" in it, thus retrieving more than I want.


PHP already builds an array for parameters that uses the array like syntax (e.g. arg[]). See How do I create arrays in a HTML <form>?


Gumbo is correct, however if you still need to perform such a task, regex can help:

if(preg_match('/^(?<key>special[0-9]+)_/ix', $post_keys[$i], $match)){
    $special_keys[] = $match['key'];
}

After which you can run array_unique() on $special_keys to get an array of groups.


For easier maintenance try this structure:

$_POST = array(
  'special' => array(
       array('time'=>, 'duration'=>, ''=>),
       array('time'=>, 'duration'=>, ''=>),
       array('time'=>, 'duration'=>, ''=>),
  ),
);

Then you can do stuff like count($_POST['special']) and $_POST['special'][1]['time']

0

精彩评论

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

关注公众号