2010-08-17 75 views
8

我發現了一個「非法偏移類型」此陣:PHP:數組可以有一個數組作爲鍵值對中的鍵嗎?

public static $CATS_AND_TYPES = array(

     // Statement Administration 
     array(self::CAT_STATEMENT_ADMIN => "Document Administration") => array(
      self::TYPE_STATEMENTS_LOADED => "Documents Loaded", 
      self::TYPE_STATEMENTS_REMOVED => "Documents Removed" 
     ), 

     // Cron Jobs 
     array(self::CAT_CRON_JOBS => "Cron Jobs") => array(
      self::TYPE_CRON_BULLETIN_RUN => "Bulletin Cron Job Ran", 
      self::TYPE_CRON_EMAILER_RUN => "Emailer Cron Job Ran", 
      self::TYPE_CRON_SURVEY_RUN => "Survey Cron Job Ran", 
      self::TYPE_CRON_JOURNEY_RUN => "Journey Cron Job Ran", 
      self::TYPE_CRON_DOCUMENT_RUN => "Document Cron Job Ran" 
     ), 

     // Global Administration 
     array(self::CAT_GLOBAL_ADMIN => "Global Administration") => array(
      self::TYPE_GLOBAL_MAINTENANCE => "Global Maintenance", 
      self::TYPE_GLOBAL_EMAIL_SENDING => "Email Sending" 
     ), 

     // Email Administration 
     array(self::CAT_EMAIL_ADMIN => "Email Administration") => array(
      self::TYPE_EMAIL_SENT => "Email Sent", 
      self::TYPE_EMAIL_RESENT => "Email Resent", 
      self::TYPE_EMAIL_REMOVED => "Email Removed" 
     ), 

     // DCVs Administration 
     array(self::CAT_DCVS_ADMIN => "DCVs Administration") => array(
      self::TYPE_DCVS_FLEX_UPDATED => "Flexible Variables Updated", 
      self::TYPE_DCVS_GLOBAL_UPDATED => "Global Variables Updated" 
     ), 

     // Video Administration 
     array(self::CAT_VIDEO_ADMIN => "Video Administration") => array(
      self::TYPE_VIDEO_ADDED => "Video Added", 
      self::TYPE_VIDEO_EDITED => "Video Edited", 
      self::TYPE_VIDEO_REMOVED => "Video Removed" 
     ), 

     // Bulletin Board Administration 
     array(self::CAT_BULLETIN_BOARD => "Bulletin Board Administration") => array(
      self::TYPE_BULLETIN_DELETED => "Message Deleted", 
      self::TYPE_BULLETIN_EDITED => "Message Edited", 
      self::TYPE_BULLETIN_ADDED => "Message Added" 
     ), 

     // User Administration 
     array(self::CAT_USER_ADMIN => "User Administration") => array(
      self::TYPE_USER_ADDED => "User Added", 
      self::TYPE_USER_ADDED_MULTI => "Multiple Users Added", 
      self::TYPE_USER_REMOVED => "User Removed", 
      self::TYPE_USER_REMOVED_MULTI => "Multiple Users Removed", 
      self::TYPE_USER_UPDATED => "User Updated" 
     ), 

     // Survey Administration 
     array(self::CAT_SURVEY_ADMIN => "Survey Administration") => array(
      self::TYPE_SURVEY_ADDED => "Survey Added", 
      self::TYPE_SURVEY_UPDATED => "Survey Updated", 
      self::TYPE_SURVEY_REMOVED => "Survey Removed", 
      self::TYPE_SURVEY_REMOVED_MULTI => "Multiple Surveys Removed" 
     ) 
    ); 

它是一種惱人再拍陣只是爲了界定什麼是這裏的鍵被定義,所以我想知道,如果這是我的問題。如果是這樣,我想我將不得不爲類別ID和字符串值做一個鍵值數組。

謝謝!

+0

請問爲什麼?在你的例子中,每個元素的鍵總是不同的。因此,不要使用'array(self :: CAT_USER_ADMIN =>'User Admin')',爲什麼不使用'self :: CAT_USER_ADMIN'? – ircmaxell 2010-08-17 16:40:10

回答

7

你會得到一個illegal offset type錯誤,因爲數組鍵只能是標量值。從陣列上的documentation

鍵可以是整數或字符串。如果一個鍵是一個整數的標準表示,它將被解釋爲這樣(即「8」將被解釋爲8,而「08」將被解釋爲「08」)。浮點鍵被截斷爲整數。

由於self::CAT_CRON_JOBS et al。看起來他們應該是常數反正,爲什麼不直接定義它們,使它們的值的說明文字,然後你可以只指定一個像

const CAT_STATEMENT_ADMIN = "Document Administration"; 

public static $CATS_AND_TYPES = array(

    // Statement Administration 
    self::CAT_STATEMENT_ADMIN => array(
    self::TYPE_STATEMENTS_LOADED => "Documents Loaded", 
    self::TYPE_STATEMENTS_REMOVED => "Documents Removed" 
), 

    // etc. 
) 

您的數組,然後你可以使用任何$CATS_AND_TYPES[self::CAT_STATEMENT_ADMIN](類中,當然)或$CATS_AND_TYPES['Document Administration']來獲得相同的數組元素。

+0

謝謝!至於你的想法,我有常量設置爲整數,我在我的數據庫中存儲爲鍵。我認爲搜索int鍵比字符串更有效,如果名稱需要更改,它不會影響數據庫數據。謝謝你的幫助! – Garrett 2010-08-17 17:14:21

+0

@加瑞特同意。我沒有意識到與數據庫值相對應的常量。在這種情況下,如果您需要描述,可以將其存儲在第二個數組中,如其他地方所述。 – 2010-08-17 17:22:23

1

編號數組只能有整數和字符串作爲鍵。

您可以使用SplObjectStorage模擬數組並使用對象作爲鍵。不過,沒有數組。

+1

或者你自己的擴展'ArrayAccess'的類... – ircmaxell 2010-08-17 17:08:02

1

我認爲這是你的問題:P

我會處理這個問題是這樣

public static $CATS_AND_TYPES = array(

    self::CAT_STATEMENT_ADMIN => array(
     self::TYPE_STATEMENTS_LOADED, 
     self::TYPE_STATEMENTS_REMOVED 
    ), 

    // ... 
); 

public static $TRANSLATIONS = array(
    self::CAT_STATEMENT_ADMIN => 'Email Administration', 
    self::TYPE_STATEMENTS_LOADED = "Documents Loaded", 
// ... 

我想這就是基本上你的意思是「讓另一個數組。」這是解決問題的正確方法,因爲它將翻譯和分層信息分開。

想象一下,當你想將你的項目翻譯成另一種語言。如果你聘請了翻譯,他不需要知道你的項目的層次結構,因爲他只應該翻譯。您還可以將$TRANSLATIONS提取到另一個文件中,以便您可以將該文件分發給99個翻譯人員,並且您的項目將在一天內成爲多種語言!

如果您非常喜歡,可以將您的數組序列化爲標量值,以便它可以充當一個關鍵字。

// Untested 
public static $CATS_AND_TYPES = array(
    serialize(array(self::CAT_STATEMENT_ADMIN=>"Documents and Administration")) => array(
     self::TYPE_STATEMENTS_LOADED => "Documents Loaded" 
     // ... 
    ) 
); 

// You can later get back the values 
foreach(self::CATS_AND_TYPES as $k=>$v) 
{ 
    $title = unserialize($k); 
    displayTitle($title[0]); 

    foreacH($v as $bar) displaySubtitle($bar); 
}