所以我可能不是最好的方法,但作为一个例子,假设我有一个像这样的数组:
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
)
什么是最好的方法?
由于
答案 0 :(得分:4)
$data = preg_split('/,\s*/', implode(',', $data));
答案 1 :(得分: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);
答案 2 :(得分: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
)