loop through $_POST variables with similar names

后端 未结 7 803
南笙
南笙 2020-12-18 09:51

I have several $_POST variables, they are

$_POST[\'item_number1\']
$_POST[\'item_number2\']

and so on

I need to write a loop tha di

7条回答
  •  误落风尘
    2020-12-18 09:58

    If you know how many do you have:

    for ($i=0; $i < $num_of_vars; $i++)
        echo $_POST['item_number'.$i]."
    ";

    UPDATE: If not:

    foreach($_POST as $k => $v) {
        $pos = strpos($k, "item_number");
        if($pos === 0)
            echo $v."
    "; }

    Gets all POST variables that are like "item_number"

    UPD 2: Changed "==" to "===" because of piotrekkr's comment. Thanks

提交回复
热议问题