如何使用sum值设置唯一id

时间:2017-03-16 06:05:23

标签: php arrays

我已经创建了数组,但我想将ib_volume和ib_comm的总和相同,并生成具有唯一contactid的新数组。

我生成相同的contact_id,但我想要只有ib_volume和ib_comm的唯一数组

我的数组如

Array
(
    [0] => Array
        (
            [contact_id] => 458
            [contact_fullName] => Karla Pearson
            [contact_email] => karla.pearson@kp.com
            [ib_level] => 1
            [ib_volume] => 0
            [ib_comm] => 0
            [parent_ib_code] => 70770
        )

    [1] => Array
        (
            [contact_id] => 460
            [contact_fullName] => Julia Thames
            [contact_email] => julia.thames@jt.com
            [ib_level] => 2
            [ib_volume] => 0
            [ib_comm] => 0
            [parent_ib_code] => 87567
        )

    [2] => Array
        (
            [contact_id] => 462
            [contact_fullName] => Bin Paulson
            [contact_email] => bin.paulson@bp.com
            [ib_level] => 2
            [ib_volume] => 0
            [ib_comm] => 0
            [parent_ib_code] => 87567
        )

    [3] => Array
        (
            [contact_id] => 464
            [contact_fullName] => Jone Oliver
            [contact_email] => jone.oliver@jo.in
            [ib_level] => 3
            [ib_volume] => 0
            [ib_comm] => 0
            [parent_ib_code] => 97000
        )

    [4] => Array
        (
            [contact_id] => 466
            [contact_fullName] => Kelvin Brian
            [contact_email] => kelvin.brian@kb.in
            [ib_level] => 4
            [ib_volume] => 2.0000
            [ib_comm] => 20
            [parent_ib_code] => 97349
        )

    [5] => Array
        (
            [contact_id] => 466
            [contact_fullName] => Kelvin Brian
            [contact_email] => kelvin.brian@kb.in
            [ib_level] => 4
            [ib_volume] => 5.0000
            [ib_comm] => 222
            [parent_ib_code] => 97349
        )

    [6] => Array
        (
            [contact_id] => 468
            [contact_fullName] => Temmis Yehishu
            [contact_email] => temmis.yehishu@ty.in
            [ib_level] => 4
            [ib_volume] => 6.0000
            [ib_comm] => 62
            [parent_ib_code] => 97349
        )

)

但我想要这样

Array
(
    [0] => Array
        (
            [contact_id] => 458
            [contact_fullName] => Karla Pearson
            [contact_email] => karla.pearson@kp.com
            [ib_level] => 1
            [ib_volume] => 0
            [ib_comm] => 0
            [parent_ib_code] => 70770
        )

    [1] => Array
        (
            [contact_id] => 460
            [contact_fullName] => Julia Thames
            [contact_email] => julia.thames@jt.com
            [ib_level] => 2
            [ib_volume] => 0
            [ib_comm] => 0
            [parent_ib_code] => 87567
        )

    [2] => Array
        (
            [contact_id] => 462
            [contact_fullName] => Bin Paulson
            [contact_email] => bin.paulson@bp.com
            [ib_level] => 2
            [ib_volume] => 0
            [ib_comm] => 0
            [parent_ib_code] => 87567
        )

    [3] => Array
        (
            [contact_id] => 464
            [contact_fullName] => Jone Oliver
            [contact_email] => jone.oliver@jo.in
            [ib_level] => 3
            [ib_volume] => 0
            [ib_comm] => 0
            [parent_ib_code] => 97000
        )

    [4] => Array
        (
            [contact_id] => 466
            [contact_fullName] => Kelvin Brian
            [contact_email] => kelvin.brian@kb.in
            [ib_level] => 4
            [ib_volume] => 2.0000+5.0000
            [ib_comm] => 20+222
            [parent_ib_code] => 97349
        )

    [5] => Array
        (
            [contact_id] => 468
            [contact_fullName] => Temmis Yehishu
            [contact_email] => temmis.yehishu@ty.in
            [ib_level] => 4
            [ib_volume] => 6.0000
            [ib_comm] => 62
            [parent_ib_code] => 97349
        )

)

5 个答案:

答案 0 :(得分:1)

你可以将contact_id作为索引值,如

if(isset(array[contact_id]))
{
   array[contact_id][new_volume]=array[contact_id][ib_volume]+array[contact_id][new_volume];
   array[contact_id][ib_comm]=array[contact_id][ib_volume]+array[contact_id][new_ib_comm];
}else
array[contact_id]=new Array(/*all new value*/)

答案 1 :(得分:1)

你可以使用foreach循环,如下所示:

$arr = Array
(
'0' => Array
    (
        'contact_id' => 458,
        'contact_fullName' => 'Karla Pearson',
        'contact_email' => 'karla.pearson@kp.com',
        'ib_level' => 1,
        'ib_volume' => 0,
        'ib_comm' => 0,
        'parent_ib_code' => 70770,
    ),

'4' => Array
    (
        'contact_id' => 466,
        'contact_fullName' => 'Kelvin Brian',
        'contact_email' => 'kelvin.brian@kb.in',
        'ib_level' => 4,
        'ib_volume' => 2.0000,
        'ib_comm' => 20,
        'parent_ib_code' => 97349,
    ),

'5' => Array
    (
        'contact_id' => 466,
        'contact_fullName' => 'Kelvin Brian',
        'contact_email' => 'kelvin.brian@kb.in',
        'ib_level' => 4,
        'ib_volume' => 5.0000,
        'ib_comm' => 222,
        'parent_ib_code' => 97349,
    )
);

$arr2 = array();

foreach ($arr as $key => $value) {
    foreach ($arr2 as $k=> &$val) {
        if ($val['contact_id'] == $value['contact_id']) {
            $val['ib_volume'] = $val['ib_volume']+$value['ib_volume'];
            $val['ib_comm'] = $val['ib_comm']+$value['ib_comm'];
            continue 2;
        }
    }
    $arr2[] = $value;
}

答案 2 :(得分:1)

请尝试此

$Array = Array
(
    '0' => Array
        (
            'contact_id' => 458,
            'contact_fullName' => 'Karla Pearson',
            'contact_email' => 'karla.pearson@kp.com',
            'ib_level' => 1,
            'ib_volume' => 0,
            'ib_comm' => 0,
            'parent_ib_code' => 70770
        ),

    '1' => Array
        (
            'contact_id' => 460,
            'contact_fullName' => 'Julia Thames',
            'contact_email' => 'julia.thames@jt.com',
            'ib_level' => 2,
            'ib_volume' => 0,
            'ib_comm' => 0,
            'parent_ib_code' => 87567
        ),

    '2' => Array
        (
            'contact_id' => 462,
            'contact_fullName' => 'Bin Paulson',
            'contact_email' => 'bin.paulson@bp.com',
            'ib_level' => 2,
            'ib_volume' => 0,
            'ib_comm' => 0,
            'parent_ib_code' => 87567
        ),

    '3' => Array
        (
            'contact_id' => 464,
            'contact_fullName' => 'Jone Oliver',
            'contact_email' => 'jone.oliver@jo.in',
            'ib_level' => 3,
            'ib_volume' => 0,
            'ib_comm' => 0,
            'parent_ib_code' => 97000
        ),

    '4' => Array
        (
            'contact_id' => 466,
            'contact_fullName' => 'Kelvin Brian',
            'contact_email' => 'kelvin.brian@kb.in',
            'ib_level' => 4,
            'ib_volume' => 2.0000,
            'ib_comm' => 20,
            'parent_ib_code' => 97349,
        ),

    '5' => Array
        (
            'contact_id' => 466,
            'contact_fullName' => 'Kelvin Brian',
            'contact_email' => 'kelvin.brian@kb.in',
            'ib_level' => 4,
            'ib_volume' => 5.0000,
            'ib_comm' => 222,
            'parent_ib_code' => 97349
        ),

    '6' => Array
        (
            'contact_id' => 468,
            'contact_fullName' => 'Temmis Yehishu',
            'contact_email' => 'temmis.yehishu@ty.in',
            'ib_level' => 4,
            'ib_volume' => 6.0000,
            'ib_comm' => 62,
            'parent_ib_code' => 97349
        ),

);

$Output = array_reduce($Array, function ($result, $item) {
        $findDuplicate = array_search($item['contact_id'], array_column($result, 'contact_id'));
        if($findDuplicate !== false) {
          $result[$findDuplicate]['ib_volume'] = $result[$findDuplicate]['ib_volume'] + $item['ib_volume'];
          $result[$findDuplicate]['ib_comm'] = $result[$findDuplicate]['ib_comm'] + $item['ib_comm'];
        }
        else
           $result[] = $item;

       return $result;
    }, array());

echo "<pre>";    
print_r($Output);  

答案 3 :(得分:1)

试试这个:

$data = array( 
  array(
    'contact_id' => '1',
    'ib_volume' => 5,
    'ib_comm' => 8),
  array(
    'contact_id' => '2',
    'ib_volume' => 10,
    'ib_comm' => 2),
  array(
    'contact_id' => '2',
    'ib_volume' => 6,
    'ib_comm' => 2),
  array(
    'contact_id' => '3',
    'ib_volume' => 8,
    'ib_comm' => 5),
  array(
    'contact_id' => '4',
    'ib_volume' => 8,
    'ib_comm' => 6),
  array(
    'contact_id' => '5',
    'ib_volume' => 3,
    'ib_comm' => 7),
  array(
    'contact_id' => '6',
    'ib_volume' => 7,
    'ib_comm' => 4),

  ); 

  $count = 0;
  foreach ($data as $value) {

      $id = $data[$count]['contact_id']; 
      $ib_volume = $data[$count]['ib_volume'];
      $ib_comm = $data[$count]['ib_comm'];          

      if ($temp == $id) {  

          $newArray = array(
              'contact_id' => $id,
              'ib_volume' => $tempib_volume + $data[$count]['ib_volume'],
              'ib_comm' => $tempib_comm + $data[$count]['ib_comm']
          );             
          unset($array[$temp-1]);

          $array[$temp] = $newArray;

      } else {
          $array[$count] = $data[$count];  
      }
      $temp = $id;  
      $tempib_volume = $data[$count]['ib_volume'];
      $tempib_comm = $data[$count]['ib_comm'];                
      $count++;

  }

  echo "<pre>";
  print_r(array_values($array));exit;

&GT;

答案 4 :(得分:0)

你也可以试试这个:

$unique_array=array();
while($row=mysqli_fetch_assoc($result)) {  //or iterate through foreach
    $contact_id = $row['contact_id'];
    if(isset($unique_array[$contact_id])) {
        $unique_array[$contact_id]['ib_volume']+=$row['ib_volume'];
        $unique_array[$contact_id]['ib_comm']+=$row['ib_comm'];
    } else {
        $unique_array[$contact_id]['contact_email'] = $row['contact_email'];
        $unique_array[$contact_id]['ib_level'] = $row['ib_level'];
        $unique_array[$contact_id]['ib_volume'] = $row['ib_volume'];
        $unique_array[$contact_id]['ib_comm'] = $row['ib_comm'];
        $unique_array[$contact_id]['parent_ib_code'] = $row['parent_ib_code'];
    }    
}