解析错误:语法错误,意外'如果' (T_IF),期待')'

时间:2015-01-30 17:26:23

标签: php arrays if-statement conditional-statements

如何在php数组中添加条件?

这是数组

$content['custom_fields'] = array(
    array( "key" => "_yoast_wpseo_focuskw", "value" => $_POST["title"] ),
    array( "key" => "_yoast_wpseo_metadesc", "value" => $_POST["titleenfa"] ),
    array( "key" => "_yoast_wpseo_metakeywords", "value" => $_POST["metakey"] ),
    if($_POST["link128"]){
        array( "key" => "_link128", "value" => "field_54b398292c295" ),
        array( "key" => "link128", "value" => $_POST["link128"] ),
    }
    if($_POST["link256"]){
        array( "key" => "_link256", "value" => "field_54b398092c294" ),
        array( "key" => "link256", "value" => $_POST["link256"] ),
    }
    if($_POST["link320"]){          
        array( "key" => "_link320", "value" => "field_54b3965495d27" ),
        array( "key" => "link320", "value" => $_POST["link320"] ),
    }
    array( "key" => "country", "value" => "USA" )
); 

但是我得到了PHP Parse错误,为什么我可以在数组中添加一个条件,会发生什么?:

  

解析错误:语法错误,意外'如果' (T_IF),期待')'

4 个答案:

答案 0 :(得分:1)

您不能在数组声明中编写if语句,但您可以做的是使用ternary运算符,如下所示:

(这里你可以选择任何一种方式)

array( "key" => "link128", "value" => (!empty($_POST["link128"])?$_POST["link128"]:"") )

或者您可以像这样使用array_push()

将一个或多个元素推送到数组末尾的位置

if($_POST["link128"])
    array_push($array, $_POST["link128"]);

答案 1 :(得分:0)

尝试修改数组代码,如下所示......

$custom_array = array(
    array( "key" => "_yoast_wpseo_focuskw", "value" => $_POST["title"] ),
    array( "key" => "_yoast_wpseo_metadesc", "value" => $_POST["titleenfa"] ),
    array( "key" => "_yoast_wpseo_metakeywords", "value" => $_POST["metakey"]     
)); 
if($_POST["link128"]){
   $custom_array = array_merge($custom_array,array(
        array( "key" => "_link128", "value" => "field_54b398292c295" ),
        array( "key" => "link128", "value" => $_POST["link128"] ),
     ));
}
if($_POST["link256"]){
   $custom_array = array_merge($custom_array,array(
        array( "key" => "_link256", "value" => "field_54b398092c294" ),
        array( "key" => "link256", "value" => $_POST["link256"] ),
   ));
 }
if($_POST["link320"]){
   $custom_array = array_merge($custom_array,array(
       array( "key" => "_link320", "value" => "field_54b3965495d27" ),
        array( "key" => "link320", "value" => $_POST["link320"] ),
    ));

}

//And finally
$content['custom_fields'] = array_merge($custom_array,array(
   array( "key" => "country", "value" => "USA" )
));

答案 2 :(得分:0)

您也可以将其写为:

$x = array();

$x[] = array( "key" => "_yoast_wpseo_focuskw", "value" => $_POST["title"] );
$x[] = array( "key" => "_yoast_wpseo_metadesc", "value" => $_POST["titleenfa"] );
$x[] = array( "key" => "_yoast_wpseo_metakeywords", "value" => $_POST["metakey"] );

if($_POST["link128"]){
    $x[] = array( "key" => "_link128", "value" => "field_54b398292c295" );
    $x[] = array( "key" => "link128", "value" => $_POST["link128"] );
}
if($_POST["link256"]){
    $x[] = array( "key" => "_link256", "value" => "field_54b398092c294" );
    $x[] = array( "key" => "link256", "value" => $_POST["link256"] );
}
if($_POST["link320"]){          
    $x[] = array( "key" => "_link320", "value" => "field_54b3965495d27" );
    $x[] = array( "key" => "link320", "value" => $_POST["link320"] );
}

$x[] = array( "key" => "country", "value" => "USA" )

$content['custom_fields'] = $x;

$x[] = $y表示“将$y推送到数组$x的末尾。”

答案 3 :(得分:0)

谢谢大家

        $content['custom_fields'] = array(
        array( "key" => "_yoast_wpseo_focuskw", "value" => $_POST["title"] ),
        array( "key" => "_yoast_wpseo_metadesc", "value" => $_POST["titleenfa"] ),
        array( "key" => "_yoast_wpseo_metakeywords", "value" => $_POST["metakey"] )
        );
        if($_POST["link128"]){
            array_push($content['custom_fields'], array( "key" => "_link128", "value" => "field_54b398292c295" ) );
            array_push($content['custom_fields'], array( "key" => "link128", "value" => $_POST["link128"] ) );
        }       
        if($_POST["link256"]){
            array_push($content['custom_fields'], array( "key" => "_link256", "value" => "field_54b398092c294" ) );
            array_push($content['custom_fields'], array( "key" => "link256", "value" => $_POST["link256"] ) );
        }   
        if($_POST["link320"]){
            array_push($content['custom_fields'], array( "key" => "_link320", "value" => "field_54b3965495d27" ) );
            array_push($content['custom_fields'], array( "key" => "link320", "value" => $_POST["link320"] ) );
        }