Arrays 如何将数组包含在条件if中?
例如,我有以下代码:Arrays 如何将数组包含在条件if中?,arrays,conditional,Arrays,Conditional,例如,我有以下代码: <?php $options = array( array( "type" => "section", "icon" => "acera-icon-preference", "title" => "General Settings", "id" => "general", "expanded" => "true" ), array(
<?php
$options = array(
array(
"type" => "section",
"icon" => "acera-icon-preference",
"title" => "General Settings",
"id" => "general",
"expanded" => "true"
),
array(
"under_section" => "ajax_login",
"type" => "checkbox",
"name" => "Who's Online Options",
"id" => array("tigu_whosonline_list", "tigu_whosonline_avatar"),
"display_checkbox_id" => "tigu_show_whosonline",
"img_desc" => "",
"options" => array("Who's Online List", "Who's Online Avatars"),
"desc" => "",
"default" => array("checked", "checked")
) );
?>
在有条件的if中,有人会这样想代码(会出错):
功能bp_处于活动状态,用于检查BuddyPress插件是否已安装。我需要的代码显示在我的wp管理面板只有当BuddyPress插件安装
谢谢 可以使用
?:
三元条件运算符:
$options = array(
function_exists('bp_is_active') // condition
? array(...stuff...) // if true
: array(), // if false
...more arrays...
);
如果条件为false,则将使用空数组。如果您不需要任何东西(与
array()
)相反),则需要更复杂的东西。我认为您只需要分号(;)
,以下代码是有效的:
if(function_exists('bp_is_active') ):
array(
"type" => "section",
"icon" => "acera-icon-preference",
"title" => "General Settings",
"id" => "general",
"expanded" => "true"
); // <<--here
endif;
如果条件为真,您是否尝试创建数组?否,您能否更具体一些?您是说您在任何情况下都创建了$options,但仅当函数bp\u处于活动状态时,才想添加带有“type”=>“section”作为第一个元素的数组?现在你的问题让人困惑。函数bp_是活动的,它检查BuddyPress插件是否已安装。我需要的代码显示在我的wp管理面板只有当BuddyPress插件安装。
$options = array(
function_exists('bp_is_active') // condition
? array(...stuff...) // if true
: array(), // if false
...more arrays...
);
if(function_exists('bp_is_active') ):
array(
"type" => "section",
"icon" => "acera-icon-preference",
"title" => "General Settings",
"id" => "general",
"expanded" => "true"
); // <<--here
endif;
if(function_exists('bp_is_active') ){
array(
"type" => "section",
"icon" => "acera-icon-preference",
"title" => "General Settings",
"id" => "general",
"expanded" => "true"
); // <<--here
}