首頁  >  文章  >  後端開發  >  數組重新組合

數組重新組合

WBOY
WBOY原創
2016-12-01 01:28:011053瀏覽

1,如何將陣列arr1轉換成arr2?

arr1 = [

<code>['tracking1','abc@qq.com','80'],
['tracking1','abc@qq.com','50'],
['tracking2','efg@qq.com','60'],
['tracking2','efg@qq.com','30'],</code>

];

arr2 = [

<code>['tracking1','abc@qq.com',['80','50']],
['tracking2','efg@qq.com',['60','30']],</code>

];

回覆內容:

1,如何將陣列arr1轉換成arr2?

arr1 = [

<code>['tracking1','abc@qq.com','80'],
['tracking1','abc@qq.com','50'],
['tracking2','efg@qq.com','60'],
['tracking2','efg@qq.com','30'],</code>

];

arr2 = [

<code>['tracking1','abc@qq.com',['80','50']],
['tracking2','efg@qq.com',['60','30']],</code>

];

思路很簡單,就是把數組的第一和第一列的值當作一個唯一key寫入,感覺樓上代碼有點多,簡潔代碼如下

<code>$arr1 = [
    ['tracking1','abc@qq.com','80'],
    ['tracking1','abc@qq.com','50'],
    ['tracking2','efg@qq.com','60'],
    ['tracking2','efg@qq.com','30'],
];

$arr2 = [];
foreach ($arr1 as $data) {
    list($account,$mail,$val) = $data;
    isset($arr2[$account.$mail]) || $arr2[$account.$mail]=[$account,$mail,[]];
    array_push($arr2[$account.$mail][2],$val);
}
$arr2 = array_values($arr2);
var_dump($arr2);</code>

我給個思路,先遍歷判斷arr1[0]是否一樣,一樣就往一個新數組中插入,在這基礎判斷[1]是否相同,相同就插入值,不同則合併成一個數組,以此類推

<code>$arr = [['tracking1','abc@qq.com','80'],
['tracking1','abc@qq.com','50'],
['tracking2','efg@qq.com','60'],
['tracking2','efg@qq.com','30']];

$finalArr = [[[]]];
$mailArr =[];
foreach ($arr as $k=>$v){
    $mailKey = array_search($v[1],$mailArr);
    if($mailKey!==false){
        array_push($finalArr[$mailKey][2],$v[2]);
    }else{
        $finalArr[$k] = $v;
        $finalArr[$k][2] = [$v[2]];
        $mailArr[$k]=$v[1];
    }
}
$finalArr = array_values($finalArr);
var_dump($finalArr);
</code>

14行功能碼,不要太簡單。

說下我的思路,題主這個數組,分開其實就是兩個部分,每個數組裡的v0、v1值,和v2值。

<code>$new = array(); 
$flag = 1;
foreach ($arr as $key => $value) {
  $newss[$value[0].'-'.$value[1]][] = $value[2];  //取出重复小标0 1的值 
  $news = array($value[0], $value[1]);
  foreach ($new as $k => $v) {
    if(!array_diff($v,$news )) { //如果
      $flag = 2;
      break;
    }
  }
  if($flag == 1) {
    $new[] = array($value[0], $value[1]);
  }
  $flag = 1;
}
foreach ($new as $key => $value) {
  $new[$key][2] = $newss[$value[0].'-'.$value[1]];
}
var_dump($new);exit;</code>

<code>    $arr1 = [    
        ['tracking1','abc@qq.com','80'],
        ['tracking1','abc@qq.com','50'],
        ['tracking2','efg@qq.com','60'],
        ['tracking2','efg@qq.com','30'],
    ];
    
    $arr2 = [
        ['tracking1','abc@qq.com',['80','50']],
        ['tracking2','efg@qq.com',['60','30']],
    ];

    define("CUSTOM_SEPA", "_");

    function t($a0){
        $r = [];
        $res = [];
        foreach($a0 as $v){
            $r[$v[0].CUSTOM_SEPA.$v[1]][] = $v[2];
        }
        foreach($r as $k=>$v){
            $s = explode(CUSTOM_SEPA, $k);
            $s[] = $v;
            $res[] = $s;
        }
        return $res;
    }

    $res = t($arr1);
    print_r($res == $arr2);</code>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn