數組重新組合

來源:互聯網
上載者:User
1,如何將數組arr1轉化成arr2?

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']],

];

回複內容:

1,如何將數組arr1轉化成arr2?

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']],

];

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

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

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

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

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

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

$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;

    $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);
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.