所以我可能不是最好的措辞,但作为一个例子,假设我有一个这样的数组:
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
)
最好的方法是什么?
谢谢
$data = preg_split('/,\s*/', implode(',', $data));
您可以使用以下解决方案:
$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);
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
)