2016-01-23 20 views
2

是否有“内联”操作,可以使这样的:是否有数组的内联“OR”运算符?

$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_fill_keys(),但它不是一个真正的“内联”的解决方案,我想能够在简单的array中查看/编辑我所有的密钥和值。

+1

*** AFAIK ***,没有没有。 – Script47

+1

不仅此...“a,b,c”=> – devpro

+0

@devpro表单不起作用。 – rockyraw

回答

0

不,没有这样的操作符特定于数组键。但是,在利用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']; 
     } 
    } 
}