解析错误:语法错误,意想不到的'if'(T
我如何在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错误,为什么我可以在数组中添加一个条件,会发生什么?
解析错误:语法错误,意外'if'(T_IF),期待')'
你不能在数组声明中写if语句,但是你可以做的是使用这样的三元运算符:
(在这里你可以有任何一个条目)
array( "key" => "link128", "value" => (!empty($_POST["link128"])?$_POST["link128"]:"") )
或者你可以像这样使用array_push()
:
它将一个或多个元素推到数组的末尾
if($_POST["link128"])
array_push($array, $_POST["link128"]);
尝试修改您的数组代码,如下所示.....
$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" )
));
感谢大家
$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"] ) );
}
链接地址: http://www.djcxy.com/p/69893.html