简体   繁体   English

有没有更紧凑,更优雅的方式编写此代码?

[英]Is there a more compact and elegant way to write this code?

The question. 问题。

I like simple and elegant code and this feels like anything but. 我喜欢简单而优雅的代码,但是感觉却不一样。 The first solution is just gross. 第一个解决方案是总的。 Functional and probably reasonably performant but gross. 机能性强,可能表现合理但总体而言。 The second is more elegant but seems like overkill for something that should be so simple. 第二个比较优雅,但是对于应该这么简单的东西似乎有点过分了。

Setup. 设定。

$recurring is an array of about 20 items. $recurring是大约20个项目的数组。 I have to map these 7 values (days of the week) to a single field. 我必须将这7个值(星期几)映射到单个字段。 Finishing both of these items with new_days = implode(',', $days); new_days = implode(',', $days);完成这两项new_days = implode(',', $days); or using them in array form depending on where this function is called. 或以数组形式使用它们,具体取决于调用此函数的位置。

Try 1 尝试1

  $days = array();
  if ($recurring['m'])
  {
    $days[] = 1;
  }
  if ($recurring['t'])
  {
    $days[] = 2;
  }
  if ($recurring['w'])
  {
    $days[] = 3;
  }
  if ($recurring['h'])
  {
    $days[] = 4;
  }
  if ($recurring['f'])
  {
    $days[] = 5;
  }
  if ($recurring['s'])
  {
    $days[] = 6;
  }
  if ($recurring['u'])
  {
    $days[] = 7;
  }

Try 2 试试2

  $map = array(
    'm' => 1,
    't' => 2,
    'w' => 3,
    'h' => 4,
    'f' => 5,
    's' => 6,
    'u' => 7,
  );
  $days = array();
  foreach ($map as $offset => $value)
  {
    if ($recurring[$offset])
    {
      $days[] = $value;
    }
  }

Any better ideas? 还有更好的主意吗?

Edit: Additional info 编辑:其他信息

The array looks something like this: 该数组如下所示:

$recurring = array(
  'id' => 27,
  'end_date' => '12-27-2005',
  'frequency' => 'W',
  'm' => 1,
  't' => 0,
  'w' => 1,
  'h' => 0
  'f' => 1,
  's' => 0,
  'u' => 0,
);

With other optional parameters. 带有其他可选参数。

You could try using a switch statement and cycle through them.. 您可以尝试使用switch语句并在它们之间循环。

foreach($recurring as $key => $value){
      switch($key){
          case 'm':
            $days[] = 1;
            break;
          case 't':
            $days[] = 2;
            break;
          case 'w':
            $days[] = 3;
            break;
          case 't':
            $days[] = 4;
            break;
          case 'f':
            $days[] = 5;
            break;
          case 's':
            $days[] = 6;
            break;
          case 's':
            $days[] = 7;
            break;
      }   
  }

for try 2: 对于尝试2:

foreach($recurring as $key => $value){
      switch($key){
          case 'm':
            $days[] = $value;
            break;
          case 't':
            $days[] = $value;
            break;
          case 'w':
            $days[] = $value;
            break;
          case 't':
            $days[] = $value;
            break;
          case 'f':
            $days[] = $value;
            break;
          case 's':
            $days[] = $value;
            break;
          case 's':
            $days[] = $value;
            break;
      }   
  }

Hope it helps, good luck 希望对您有帮助,祝您好运

Any better ideas? 还有更好的主意吗?

Whether this is better is a matter of taste, but at least it's different: 这是否更好取决于口味,但至少有所不同:

$recurring = array(
  'id' => 27,
  'end_date' => '12-27-2005',
  'frequency' => 'W',
  'm' => 1,
  't' => 0,
  'w' => 1,
  'h' => 0,
  'f' => 1,
  's' => 0,
  'u' => 0
);

$days = $recurring;
array_splice($days, 0, 3, 0);   # replace leading 3 elements by one element 0
$days = array_keys(array_filter(array_values($days)));

It reduces the given array to the days' elements (plus a leading 0 to give the wanted day numbering from 1 on), changes the keys to indexes, filters the elements to keep the 1 s, and takes the remaining indexes, which are the wanted day numbers. 它将给定数组减少为天的元素(加上前导0以将想要的天数从1开始),更改索引的键,过滤元素以保留1 s,然后获取其余的索引,即想要的天数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM