0

所以我可能不是最好的措辞,但作为一个例子,假设我有一个这样的数组:

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

我想要做的是得到这个:

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

最好的方法是什么?

谢谢

4

3 回答 3

4
$data = preg_split('/,\s*/', implode(',', $data));
于 2013-09-26T20:18:12.320 回答
1

您可以使用以下解决方案:

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

演示!


原答案:

$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);
于 2013-09-26T19:56:37.197 回答
0
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
)
于 2013-09-26T21:49:55.183 回答