Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 是否有“内联”选项;或;数组的运算符?_Php_Arrays - Fatal编程技术网

Php 是否有“内联”选项;或;数组的运算符?

Php 是否有“内联”选项;或;数组的运算符?,php,arrays,Php,Arrays,是否有任何“内联”运算符可以执行此操作: $class_map = array( 'a' => 'long text', 'b' => 'long text', 'c' => 'long text', 'd' => 'other text', 'e' => 'different text' ); 大概是: $class_map = array( 'a' OR `b` OR `c` => 'long text' 'd'

是否有任何“内联”运算符可以执行此操作:

$class_map = array(
    'a' => 'long text',
    'b' => 'long text',
    'c' => 'long text',
    'd' => 'other text',
    'e' => 'different text'
);
大概是:

$class_map = array(
'a' OR `b` OR `c` => 'long text'
'd' => 'other text',
'e' => 'different text'
);

我知道
array\u fill\u keys()
,但它不是真正的“内联”解决方案,我希望能够在简单的
array

中查看/编辑我的所有键和值不,没有特定于数组键的此类运算符。不过,可能还有其他方法可以实现您想要的功能,更简单的方法是利用PHP中数组的特性

例如

$class_map = [
    'a' => [
        'alias' => ['b','c',],
        'value' => 'long text',
    ],
    'd' => 'other text',
    'e' => 'different text',
];
现在您的数组可以这样读取

foreach($class_map as $key => $value) {
    if (is_array($value)) {
        // has aliases...
        foreach($value['alias'] as $v) {
            // do stuff with aliases here
        }
    } else {
        // has no aliases
    }
}
为了搜索别名,您可以按照以下方式执行操作

function searchClassMap($className, Array $class_map)
{
    if (isset($class_map[$className])) {
        // if the className already is a key return its value
        return is_array($class_map[$className])
               ? $class_map[$className]['value']
               : $class_map[$className];
    }
    // otherwise search the aliases...
    foreach($class_map as $class => $data) {
        if (!is_array($data) || !isset($data['alias'])) {
            continue;
        }

        if (in_array($className, $data['alias'])) {
            return $data['value'];
        }
    }
}

好了,没有,只有这个。。。“a,b,c”=>@devpro该表单不起作用。因此,您所追求的基本上是一种神奇的语法,当您有多个具有相同值的条目时,可以减少键入?没有神奇的语法;但您可以将“长文本”定义为常量,并在构建数组时仅引用该内容