将List组件转换为list

Is there an easy way to expand out a list to individual components? E.g. if a client requests the following lines: 1,5,6,10-15,20,21 is there an easy way using PHP (or MySQL directly) to convert this to a list of: 1,5,6,10,11,12,13,14,15,20,21.

I am thinking I can move each element into an array and then look for the '-' character in an 'a-b' component and use that to run a loop process to add numbers from 'a' through to 'b' but there may be an easier way.

Your process is correct and very understandable, and there seems to be no built-in function to do it. Using the range function makes it easier than with a regular for loop.

$values = explode (',', $str);

foreach ($values as $val) {
  if (strpos($val, '-') !== false) {
    $range_values = explode ('-', $val);
    foreach (range($range_values[0], $range_values[1]) as $val_to_add) {
      $values[] = $val_to_add;
    }
  }
}

sort($values);

$str = implode (',', $values);