duancaiyi7567
duancaiyi7567
2014-04-17 10:36
浏览 85
已采纳

在PHP中增加多个变量For循环

I have a code that separates first from last names, that I am using on a form that could have up to ten names. Right now I have simply replicated the code multiple times, manually updating the number, but figure there has to be a way to loop this function.

The basic code is:

if (!empty($_POST['name2'])) {
    $name2 = ucwords(strtolower($_POST['name2']));
    $parser = new HumanNameParser_Parser($name2);
    $fname2 = $parser->getFirst();
    $lname2 = $parser->getLast();
}

As the fname and lname variables are used elsewhere I need to stick with the naming.

I was thinking to use a For:

for ($i=2; $i <= 6; $i++)
{
    if (!empty($_POST['name($i)'])) {
        $name($i) = ucwords(strtolower($_POST['name($i)']));
        $parser = new HumanNameParser_Parser($name($i));
        $fname($i) = $parser->getFirst();
        $lname($i) = $parser->getLast();
    }
    else
    {
        break;
    }
}

But I am having a tough time figuring what I should actually use for the ($i) in my example. I tried .$i and [$i] but keep getting errors. And not sure if the way I should code it in the $_POST would be different than the variable $name.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dsg56465
    dsg56465 2014-04-17 10:42
    已采纳

    for the $_POST variable you can use $_POST['name' . $i] and for the name variable you can just replace it with ${'name' . $i};

    so it will be like this

    for ($i=2; $i <= 6; $i++)
    {
    if (!empty($_POST['name' . $i])) {
        ${'name' . $i} = ucwords(strtolower($_POST['name' . $i]));
        $parser = new HumanNameParser_Parser(${'name' . $i});
        ${'fname' . $i} = $parser->getFirst();
        ${'lname' . $i} = $parser->getLast();
      }  
    else 
      { 
        break;
      }
    }
    
    点赞 评论
  • dousha2020
    dousha2020 2014-04-17 10:39

    Could you try this?:

    for ($i=2; $i <= 6; $i++)
    {
    if (!empty($_POST["name$i"])) {
        $name[$i] = ucwords(strtolower($_POST["name$i"]));
        $parser = new HumanNameParser_Parser($name[$i]);
        $fname[$i] = $parser->getFirst();
        $lname[$i] = $parser->getLast();
      }  
    else 
      { 
        break;
      }
    }
    
    点赞 评论
  • dsa88885555
    dsa88885555 2014-04-17 10:39

    You can use $_POST['name'.$i] to get that index. I would suggest something like this:

    $people = array();
    for( $i=2; $i<=6; $i++) {
        if( empty($_POST['name'.$i])) continue;
        $name = ucwords(strtolower($_POST['name'.$i]));
        $parser = new HumanNameParser_Parser($name);
        $people[$i] = array("first"=>$parser->getFirst(),"last"=>$parser->getLast());
    }
    

    This will result in a $people array, which is more usable than variable variables.

    点赞 评论
  • dongyata3336
    dongyata3336 2014-04-17 10:39

    Something like this?

    $name = array();
    $fname = array();
    $lname = array();
    for ($i=2; $i <= 6; $i++)
    {
        if (!empty($_POST['name_'.$i])) {
            $name[$i] = ucwords(strtolower($_POST['name_'.$i]));
            $parser = new HumanNameParser_Parser($name[$i]);
            $fname[$i] = $parser->getFirst();
            $lname[$i] = $parser->getLast();
        }  
        else 
        { 
            break;
        }
    }
    
    点赞 评论

相关推荐