如何在数组中使用if语句?

时间:2017-10-28 作者:Faruk

首先对不起我的英语。我希望你能理解我。

我有这样一个数组:

$fields =  array(
  \'author\' => \'<div class="form-group"><input class="form-control" id="author" name="author" type="text" placeholder="Name \'.( $req ? \'*\' : \'\' ) .\'" value="\' . esc_attr( $commenter[\'comment_author\'] ) . \'" \' . $aria_req . \' /></div>\',
  \'email\'  => \'<div class="form-group"><input class="form-control" id="email" name="email" type="text" placeholder="E-mail \'.( $req ? \'*\' : \'\' ) .\'" value="\' . esc_attr(  $commenter[\'comment_author_email\'] ) . \'" size="30"\' . $aria_req . \' /></div>\',
  \'url\'  => \'<div class="form-group"><input class="form-control" id="url" name="url" type="text" placeholder="Website \'.( $req ? \'*\' : \'\' ) .\'" value="\' . esc_attr(  $commenter[\'comment_author_url\'] ) . \'" size="30"\' . $aria_req . \' /></div>\',
);
我想在这个数组中使用if语句。像这样:

$fields =  array(
   \'author\' => \'<div class="form-group"><input class="form-control" id="author" name="author" type="text" placeholder="Name \'.( $req ? \'*\' : \'\' ) .\'" value="\' . esc_attr( $commenter[\'comment_author\'] ) . \'" \' . $aria_req . \' /></div>\',
   \'email\'  => \'<div class="form-group"><input class="form-control" id="email" name="email" type="text" placeholder="E-mail \'.( $req ? \'*\' : \'\' ) .\'" value="\' . esc_attr(  $commenter[\'comment_author_email\'] ) . \'" size="30"\' . $aria_req . \' /></div>\',
   if($showURL==1){\'url\'  => \'<div class="form-group"><input class="form-control" id="url" name="url" type="text" placeholder="Website \'.( $req ? \'*\' : \'\' ) .\'" value="\' . esc_attr(  $commenter[\'comment_author_url\'] ) . \'" size="30"\' . $aria_req . \' /></div>\'},
);
我正在尝试,但它显示了一个语法错误:

$fields =  array(
    \'author\' => \'<div class="form-group"><input class="form-control" id="author" name="author" type="text" placeholder="Adınız \'.( $req ? \'*\' : \'\' ) .\'" value="\' . esc_attr( $commenter[\'comment_author\'] ) . \'" \' . $aria_req . \' /></div>\',
    \'email\'  => \'<div class="form-group"><input class="form-control" id="email" name="email" type="text" placeholder="E-posta adresiniz \'.( $req ? \'*\' : \'\' ) .\'" value="\' . esc_attr(  $commenter[\'comment_author_email\'] ) . \'" size="30"\' . $aria_req . \' /></div>\',
       (($showURL==1) ?  \'url\'  => \'<div class="form-group"><input class="form-control" id="url" name="url" type="text" placeholder="Website \'.( $req ? \'*\' : \'\' ) .\'" value="\' . esc_attr(  $commenter[\'comment_author_url\'] ) . \'" size="30"\' . $aria_req . \' /></div>\' : \'\' ) ,
);
我收到以下错误:解析错误:语法错误,在C:\\wamp\\www\\blabla\\comments中出现意外的“=>”(T\\u双箭头)。php第62行

如何在此数组中使用此if语句?有人能帮我吗?

1 个回复
最合适的回答,由SO网友:Alexander Holsgrove 整理而成

Try this Omer

$fields =  array(
    \'author\' => \'<div class="form-group"><input class="form-control" id="author" name="author" type="text" placeholder="Name \'.( $req ? \'*\' : \'\' ) .\'" value="\' . esc_attr( $commenter[\'comment_author\'] ) . \'" \' . $aria_req . \' /></div>\',
    \'email\'  => \'<div class="form-group"><input class="form-control" id="email" name="email" type="text" placeholder="E-mail \'.( $req ? \'*\' : \'\' ) .\'" value="\' . esc_attr(  $commenter[\'comment_author_email\'] ) . \'" size="30"\' . $aria_req . \' /></div>\',
);

if($showURL == 1) {
    $fields[\'url\'] = \'<div class="form-group"><input class="form-control" id="url" name="url" type="text" placeholder="Website \'.( $req ? \'*\' : \'\' ) .\'" value="\' . esc_attr(  $commenter[\'comment_author_url\'] ) . \'" size="30"\' . $aria_req . \' /></div>\';
}
结束