我有一个具有混合值的数组,如下所示:
Array
(
[0] => 93,103
[1] => 93
[2] => 103
[3] => 95,45,93,18
[4] => 12,45
)
我想将它转换为Array,每个值都是唯一的,如下所示:
Array
(
[0] => 93
[1] => 103
[2] => 18
[3] => 12
[4] => 95
[5] => 45
)
这样做的好方法是什么?
答案 0 :(得分:2)
如下所示:
$array = array(
'93,103',
'93',
'103',
'95,45,93,1',
'12,45'
);
$result = array_unique(call_user_func_array('array_merge', array_map(function($e) {
return explode(',', $e);
}, $array)));
var_dump($result);
结果:
array(6) {
[0]=>
string(2) "93"
[1]=>
string(3) "103"
[4]=>
string(2) "95"
[5]=>
string(2) "45"
[7]=>
string(1) "1"
[8]=>
string(2) "12"
}
答案 1 :(得分:1)
$yourArray = array(
'93,103',
'93',
'103',
'95,45,93,18',
'12,45',
);
$new = array();
foreach ($yourArray as $val) {
$new = array_merge($new, explode(",", $val));
}
foreach (array_unique($new) as $n){
$result[] = $n;
}
print_r($result);
答案 2 :(得分:1)
你只是使用内爆并爆炸它会对你有用。
$a=Array
(
[0] => 93,103
[1] => 93
[2] => 103
[3] => 95,45,93,18
[4] => 12,45
)
$b=implode(",",$a);
$c=explode(",", $b);
$c=Array
(
[0] => 93
[1] => 103
[2] => 18
[3] => 12
[4] => 95
[5] => 45
)