PHP:數組是否可以將數組作為鍵值對中的鍵?

[英]PHP: Can an array have an array as a key in a key-value pair?


I'm getting an "Illegal offset type" for this array:

我得到了這個數組的“非法偏移類型”:

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

it's kind of annoying to make another array just to define what is being defined in the keys here, so i was wondering if that was my problem. if it is, i guess i'll have to make a key-value array for the categories ids and string values.

制作另一個數組只是為了定義鍵中定義的內容,這有點令人討厭,所以我想知道這是不是我的問題。如果是,我想我將不得不為類別ID和字符串值創建一個鍵值數組。

thanks!

謝謝!

3 个解决方案

#1


8  

You're getting an illegal offset type error because array keys can only be scalar values. From the documentation on arrays:

您將收到非法的偏移類型錯誤,因為數組鍵只能是標量值。從數組的文檔:

A key may be either an integer or a string. If a key is the standard representation of an integer, it will be interpreted as such (i.e. "8" will be interpreted as 8, while "08" will be interpreted as "08"). Floats in key are truncated to integer.

密鑰可以是整數或字符串。如果一個鍵是整數的標准表示,它將被解釋為這樣(即“8”將被解釋為8,而“08”將被解釋為“08”)。鍵中的浮點數被截斷為整數。

Since self::CAT_CRON_JOBS et al. seem like they should be constants anyways, why not just define them so that their value is the description text, and then you could just specify your array like

自從:: CAT_CRON_JOBS等。似乎它們應該是常量,為什么不定義它們以使它們的值是描述文本,然后你可以只指定你的數組

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.
)

And then you could use either $CATS_AND_TYPES[self::CAT_STATEMENT_ADMIN] (within the class, of course) or $CATS_AND_TYPES['Document Administration'] to get the same array element.

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

#2


1  

No. Arrays can only have integers and strings as keys.

不可以。數組只能有整數和字符串作為鍵。

You can simulate arrays and use objects as keys with SplObjectStorage. No arrays, though.

您可以使用SplObjectStorage模擬數組並將對象用作鍵。但是沒有數組。

#3


1  

I think it's your problem :P

我認為這是你的問題:P

I would approach the problem like this

我會像這樣處理問題

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",
// ...

I guess that's basically what you meant by "make another array." This is the correct approach to the problem because it separates translation and the hierarchical information.

我猜這基本上就是你所說的“制造另一個陣列”。這是解決問題的正確方法,因為它將翻譯和層次信息分開。

Imagine when you want to translate your project to another language. If you hired a translator, he shouldn't need to know your project's hierarchical structure because he should only translate. You can also extract $TRANSLATIONS into another file so that you can distribute that file to 99 translators and your project will be multilingual in one day!

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

If you are so inclined, you can serialize your array into a scalar value so it can act as a key.

如果您如此傾向,可以將數組序列化為標量值,以便它可以作為鍵。

// 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);
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/08/17/84d2d673ba5115c6f5da56494c95535b.html



 
  © 2014-2022 ITdaan.com 联系我们: