我为这个数组得到了“非法偏移类型”:
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 :(得分:8)
您收到illegal offset type
错误,因为数组键只能是标量值。来自数组上的documentation:
键可以是整数或字符串。如果一个键是整数的标准表示,它将被解释为这样(即“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']
来获得相同的数组元素。
答案 1 :(得分:2)
没有。数组只能将整数和字符串作为键。
您可以使用SplObjectStorage
模拟数组并将对象用作键。但是没有数组。
答案 2 :(得分: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);
}