Parse error: syntax error, unexpected 'if' (T
How can I add a condition inside a php array?
Here is the array
$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" )
);
But I get the PHP Parse error, why I can add a condition inside the array, what's happen??:
Parse error: syntax error, unexpected 'if' (T_IF), expecting ')'
You can't write a if statement in a array declaration, but what you could do is to use a ternary operator like this:
(Here you would have either way a entry)
array( "key" => "link128", "value" => (!empty($_POST["link128"])?$_POST["link128"]:"") )
OR you can use array_push()
like this:
Where it push's one or more elements to the end of your array
if($_POST["link128"])
array_push($array, $_POST["link128"]);
Try to modify your array code like following.....
$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/69894.html
上一篇: CLASS)只在远程(不在本地)