9

我得到这个数组的“非法偏移类型”:

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 和字符串值创建一个键值数组。

谢谢!

4

3 回答 3

8

你得到一个illegal offset type错误,因为数组键只能是标量值。从有关数组的文档中:

键可以是整数或字符串。如果键是整数的标准表示,它将被解释为这样(即“8”将被解释为 8,而“08”将被解释为“08”)。键中的浮点数被截断为整数。

由于self::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.
)

然后你可以使用$CATS_AND_TYPES[self::CAT_STATEMENT_ADMIN](当然是在类中)或$CATS_AND_TYPES['Document Administration']获取相同的数组元素。

于 2010-08-17T16:33:42.583 回答
3

不可以。数组只能有整数和字符串作为键。

您可以模拟数组并将对象用作键SplObjectStorage。但是没有数组。

于 2010-08-17T16:47:29.070 回答
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);
}
于 2010-08-17T17:05:44.883 回答