2014-01-22 39 views
-2

我有混合值的数组,像这样:如何使阵列混合值独特的阵列

Array 
(
[0] => 93,103 
[1] => 93 
[2] => 103 
[3] => 95,45,93,18 
[4] => 12,45 
) 

,我想将它转换成数组,每个值是唯一的,就像这样:

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
$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); 
+0

谢谢,它滑动一些数组键,因为他们是相同的。 – user2706762

+0

你想重新分配钥匙? –

+0

是,[0] [1] [2] ..... – user2706762

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 
)