douyou2368
douyou2368
2013-10-10 09:18

将数组中的元素按两个对分配,然后找出差值和总和

已采纳

Let's say I have this array

$number = [2,1,4,3,6,2];
  1. First pair the elements on an array by two's and find their difference so this is the output in the first requirement

    $diff[] = [1,1,4];
    
  2. Second sum all the difference this is the final output

    $sum[] = [6];
    

Conditions:

  1. the array size is always even
  2. the first element in a pair is always greater than the second one, so their is no negative difference

What I've done so far is just counting the size of an array then after that I don't know how to pair them by two's. T_T

Is this possible in php? Is there a built in function to do it?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

7条回答

  • dqsvf28682 dqsvf28682 8年前

    One line:

    $number = [2,1,4,3,6,2];
    $total = array_sum(array_map(function ($array) {
        return current($array) - next($array); 
    }, array_chunk($number, 2)));
    echo $total;
    
    点赞 评论 复制链接分享
  • doulang7699 doulang7699 8年前

    I have got this far it gives the required solution.

    $arr = array(2,1,4,3,6,2);
    $temp = 0;
    $diff = array();
    foreach ($arr as $key => $value) {
        if($key % 2 == 0) {
            $temp = $value;
        }
        else {
            $diff[] = $temp - $value;
        }
    }
    
    print_R($diff);
    print 'Total :' . array_sum($diff);
    

    Note : Please update if any one knows any pre-defined function than can sorten this code.

    点赞 评论 复制链接分享
  • dongxiqian2787 dongxiqian2787 8年前

    Try this one:

        $number = array(2,1,4,3,6,2);
    
        $diff = array();
        $v3 = 0;
        $i=1;
        foreach($number as $val){
        if ($i % 2 !== 0) {
            $v1 = $val;
        }
        if ($i % 2 === 0) {
            $v2 = $val;
            $diff[] = $v1-$v2;
            $v3+= $v1-$v2;       
            }
        $i++;
        }
        print $v3;//total value
        print_r($diff); //diff value array
    
    点赞 评论 复制链接分享
  • dongwu8050 dongwu8050 8年前

    Please check and see if this works for you.

        <?php        
        $sum=0;
        $number = array(2,1,4,3,6,2);
        for ($i=0;$i<=count($number);$i++) {
        if ($i%2 == 1 ) {  
            $sum = $sum + $number[$i-1] - $number[$i];
        }
        }
    
        print $sum;
        ?>
    
    点赞 评论 复制链接分享
  • dter8514 dter8514 8年前

    If you want all the different stages kept,

    $numbers = [2,1,4,3,6,2];
    $diff = [];
    for($i=0,$c=count($numbers);$i<$c;$i+=2)
    {
        $diff[] = $numbers[$i]-$numbers[$i+1];
    }
    $sum = array_sum($diff);
    

    Else, to just get the total and bypass the diff array:

    $numbers = [2,1,4,3,6,2];
    $total = 0;
    for($i=0,$c=count($numbers);$i<$c;$i+=2)
    {
        $total += $numbers[$i]-$numbers[$i+1];
    }
    
    点赞 评论 复制链接分享
  • dpjjmo3079 dpjjmo3079 8年前

    This should work fine:

    <?
    $number = array(2,1,4,3,6,2);
    for($i=0;$i<count($number); $i+=2){
        $dif[] = $number[$i] - $number[$i+1];
    }
    print_r($dif);
    $sum = 0;
    foreach ($dif as $item){
        $sum += $item;
    }
    echo 'SUM = '.$sum;
    ?>
    

    Working CODE

    点赞 评论 复制链接分享
  • doushengyou2617 doushengyou2617 8年前

    Well with your conditions in mind I came to the following

    $number = [2,1,4,3,6,2];
    $total = 0;
    for($i = 0; $i < count($number); $i+=2) {
        $total += $number[$i] - $number[$i + 1];
    }
    
    点赞 评论 复制链接分享

相关推荐