用逗号分隔的PHP数组

so I am probably not phrasing this the best way but as an example, assume I have an array like this:

Array
(
   [0] => 1,24,5
   [1] => 4
   [2] => 88, 12, 19, 6
)

And what I want to do is get this:

Array
(
   [0] => 1
   [1] => 24
   [2] => 5
   [3] => 4
   [4] => 88
   [5] => 12
   [6] => 19
   [7] => 6
)

What would be the best method?

Thanks

You can use the following solution:

$result = array();
foreach($inputArray as $value) {
    $result = array_merge($result, explode(',', $value));
}

Demo!


Original answer:

$arr = array('1,24,5', 4, '88, 12, 19, 6');
$result = array();

foreach ($arr as $value) {
    if(strpos($value, ',') !== FALSE) {
        $result = array_merge($result, explode(',', $value));
        $result = array_map('trim', $result); // trim whitespace
    }
    else {
        $result[] = trim($value);
    }
}

print_r($result);
$data = preg_split('/,\s*/', implode(',', $data));
Array(
  '1,24,5',
  '4',
  '88,12,19,6'
);


$new_arr = explode(',',implode(',',array_values($old_arr)));


Array
(
  [0] => 1
  [1] => 24
  [2] => 5
  [3] => 4
  [4] => 88
  [5] => 12
  [6] => 19
  [7] => 6
)