2015-10-07 86 views
0

我使用codeigniter 3.x版本。php陣列合併後單獨逗號合併相同的密鑰

這是傳遞給ajax的值。

Array ( 
    [0] => 10:4 
    [1] => 11:1 
    [2] => 12:2 
    [3] => 13:3 
    [4] => 14:4 
    [5] => 15:5 
    [6] => 16:4 
    [7] => 17:3 
    [8] => 18:2 
    [9] => 19:1 
    [10] => 1:4 // duplicate 
    [11] => 1:5 // duplicate 
    [12] => 20:2 
    [13] => 21:3 
    [14] => 22:4 
    [15] => 23:5 
    [16] => 24:4 // duplicate 
    [17] => 24:5 // duplicate 
    [18] => 25:2 
    [19] => 2:5 
    [20] => 3:4 
    [21] => 4:4 
    [22] => 5:4 
    [23] => 6:3 
    [24] => 7:2 
    [25] => 8:3 
    [26] => 9:2 // duplicate 
    [27] => 9:4 // duplicate 
) 

我要插入到數據庫中,處理數組

首先,排序使用natsort($數據)

1:4 
1:5 
2:5 
3:4 
4:4 
5:4 
6:3 
7:2 
8:3 
9:2 
10:4 
11:1 
12:2 
13:3 
14:4 
15:5 
16:4 
17:3 
18:2 
19:1 
20:2 
21:3 
22:4 
23:5 
24:4 
24:5 
25:2 
25:5 

這是令人滿意的。 但

1:4 
1:5 

24:4 
24:5 

25:2 
25:5 

是有重複的值

我的問題是

1:4 
1:5 
24:4 
24:5 
25:2 
25:5 

1:4,5 
2:5 
3:4 
. 
. 
24:4,5 
25:2,5 

如何來在PHP處理?

+0

重複從那裏值獲取所有第一部分和設定值到所有重複?從你的數據庫? –

回答

0
$keys = array(); 
$list = array(
    '1:4','1:5','2:1','3:4','4:1','4:2' 
); 

// Seach the keys 
foreach($list as $item){ 
    $key = substr($item,0,strpos($item, ':')); 
    $val = substr($item,strpos($item, ':')+1); 
    if(!isset($keys[$key])) $keys[$key] = array(); 
    $keys[$key][] = $val; 
} 

//And now you can handle how do you want 
$final = array(); 
foreach($keys as $key => $value){ 
    $final[$key] = implode(',',$value); 
} 

var_dump($list); 
var_dump($keys); 
var_dump($final); 

===================== OUTPUT =====================

$list: 
    array (size=6) 
     0 => string '1:4' (length=3) 
     1 => string '1:5' (length=3) 
     2 => string '2:1' (length=3) 
     3 => string '3:4' (length=3) 
     4 => string '4:1' (length=3) 
     5 => string '4:2' (length=3) 

$keys: 
    array (size=4) 
     1 => 
     array (size=2) 
      0 => string '4' (length=1) 
      1 => string '5' (length=1) 
     2 => 
     array (size=1) 
      0 => string '1' (length=1) 
     3 => 
     array (size=1) 
      0 => string '4' (length=1) 
     4 => 
     array (size=2) 
      0 => string '1' (length=1) 
      1 => string '2' (length=1) 

$final: 
    array (size=4) 
     1 => string '4,5' (length=3) 
     2 => string '1' (length=1) 
     3 => string '4' (length=1) 
     4 => string '1,2' (length=3) 
3

這可能幫助 -

$new= []; 
foreach($ar as $v) { 
    $temp = explode(':', $v); 
    $new[$temp[0]][] = $temp[1]; 
} 
foreach($new as $k => $v) { 
    $new[$k] = $k . ':' . implode(',', $v); 
} 

DEMO

+0

感謝您的回答 –

0

您可以兩步實現它..

  1. 通過使關鍵
  2. 逆向工程實現結果

    $a=array('1:4' ,'1:5' ,'24:4','24:5','25:2','25:5');//input here 
    $temp=array(); 
    $result=array(); 
    foreach($a as $v) 
    { 
    $part=explode(":",$v); 
    if(isset($temp[$part[0]])) 
        $temp[$part[0]].=",".$part[1]; 
    else 
        $temp[$part[0]]=$part[1]; 
    } 
    foreach($temp as $k=>$v) 
    { 
    $result[]=$k.":".$v; 
    } 
    print_r($result); 
    

OUPTUT

Array ([0] => 1:4,5 [1] => 24:4,5 [2] => 25:2,5)