2013-04-14 69 views
0

例如,我有以下代碼:如何在有條件的數組中包含數組?

<?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") 

)); 

?> 

有一個以上陣列的機器,但我縮短的代碼,以節省空間。現在的問題是: 我怎麼能包括在條件如果 somethink這樣的代碼只是將下面的代碼

array(
      "type" => "section", 
      "icon" => "acera-icon-preference", 
      "title" => "General Settings", 
      "id" => "general", 
      "expanded" => "true" 
     ), 

(即出現錯誤):

if(function_exists('bp_is_active')): 

    array(
       "type" => "section", 
       "icon" => "acera-icon-preference", 
       "title" => "General Settings", 
       "id" => "general", 
       "expanded" => "true" 
      ), 

endif; 

bp_is_active它檢查功能的BuddyPress插件已安裝。只有在BuddyPress插件已安裝的情況下,我需要在我的wp管理面板上顯示該代碼。

謝謝!

+0

你嘗試,如果'if'條件爲真創建陣列? – egig

+0

不,你能更具體嗎? –

+0

是不是說你在任何情況下都創建了$ options,但是隻有當函數bp_is_active存在時纔想添加帶有「type」=>「section」的數組作爲第一個元素?現在你的問題很混亂。 –

回答

0

您可以使用?:三元條件運算符:

$options = array(
    function_exists('bp_is_active') // condition 
    ? array(...stuff...)   // if true 
    : array(),     // if false 
    ...more arrays... 
); 

如果條件爲假,將使用空數組。如果你不需要任何東西(而不是array()),你需要更復雜的東西。

0

我想你只需要分號(;),下面的代碼是有效的:

if(function_exists('bp_is_active')): 
    array(
      "type" => "section", 
      "icon" => "acera-icon-preference", 
      "title" => "General Settings", 
      "id" => "general", 
      "expanded" => "true" 
     ); // <<--here 

    endif; 

或不endif

 if(function_exists('bp_is_active')){ 
    array(
      "type" => "section", 
      "icon" => "acera-icon-preference", 
      "title" => "General Settings", 
      "id" => "general", 
      "expanded" => "true" 
     ); // <<--here 
    } 
相關問題