我有这个数组:
array(9) {
[0]=>
string(5) ",103,"
[1]=>
string(4) ",12,"
[2]=>
string(5) ",123,"
[3]=>
string(5) ",135,"
[4]=>
string(5) ",148,"
[5]=>
string(4) ",27,"
[6]=>
string(16) ",30,31,38,39,40,"
[7]=>
string(4) ",35,"
[8]=>
string(4) ",94,"
}
如何将它合并到这样的数组?
$a = array(12, 27, 30, 31, 35, 38, 39, 40, ...)
我试过但却毫无头绪......
答案 0 :(得分:1)
使用sort
和$arr = [",103,",",12,",",123,",",135,",",148,",",27,",",30,31,38,39,40,",",35,",",94,"];
preg_match_all('/\d+/', implode("", $arr), $m);
$result = $m[0];
sort($result);
print_r($result);
函数的解决方案:
Array
(
[0] => 12
[1] => 27
[2] => 30
[3] => 31
[4] => 35
[5] => 38
[6] => 39
[7] => 40
[8] => 94
[9] => 103
[10] => 123
[11] => 135
[12] => 148
)
输出:
tabindex="-1"
答案 1 :(得分:0)
<?php
$test = array(",103,", ",12,", ",123,", ",135,", ",148,", ",27,",",30,31,38,39,40,", ",35,", ",94,");
$values = array_filter(explode(',', implode(",", $test)));
sort($values);
print_r($values);
将输出
Array
(
[0] => 12
[1] => 27
[2] => 30
[3] => 31
[4] => 35
[5] => 38
[6] => 39
[7] => 40
[8] => 94
[9] => 103
[10] => 123
[11] => 135
[12] => 148
)
答案 2 :(得分:0)
你可以遍历数组,然后用逗号分解它。然后,如果该值不是空的sting,则将其添加到新数组。
$arr = [",103,", ",12,", ",123,", ",135,", ",148,", ",27,", ",30,31,38,39,40,", ",35,", ",94,"];
$new = array();
foreach ($arr as $val) {
foreach (explode(",", $val) as $char) {
if ($char != '') {
array_push($new, $char);
}
}
}
答案 3 :(得分:-1)
试试这个:
sort(explode(',', str_replace(',,', ',', implode('', $a))));