Home  >  Article  >  Backend Development  >  Array regrouping

Array regrouping

WBOY
WBOYOriginal
2016-12-01 01:28:011023browse

1, How to convert array arr1 into 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>

];

Reply content:

1, How to convert array arr1 into 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>

];

The idea is very simple, that is, write the value of the first and first columns of the array as a unique key. It feels like there is a lot of code upstairs. The concise code is as follows

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

I will give you an idea. First traverse to determine whether arr1[0] is the same. If they are the same, insert them into a new array. Based on this, determine whether [1] is the same. If they are the same, insert the values. If they are different, merge them into an array. And so on.

<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 lines of functional code, don’t be too simple.

Let me tell you my thoughts. The array in question is actually divided into two parts, the v0, v1 values, and v2 values ​​in each array.

<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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn