繁体   English   中英

PHP的总和与唯一ID数组选择键

[英]php sum selected keys in array with unique id

我有一个名为print_r($list) php数组,您可以在下面看到输出;

 Array (     
    [userid] => 1042    
    [picture] => 7a86b24.jpg     
    [share] => 6     
    [sharedate] => 2017-10-02 ) 

Array (     
    [userid] => 1042
    [picture] => 7a86b24.jpg
    [share] => 1    
    [sharedate] => 2017-10-02 ) 

Array (     
    [userid] => 1042     
    [picture] => 7a86b24.jpg     
    [share] => 56    
    [sharedate] => 2017-10-02 ) 

Array (     
     [userid] => 1007    
     [picture] => 3a83a6d.jpg     
     [share] => 136    
     [sharedate] => 2017-10-02 )

 Array (     
     [userid] => 1007    
     [picture] => 3a83a6d.jpg    
     [share] => 18    
     [sharedate] => 2017-10-02 )

 Array (     
    [userid] => 1007
    [picture] => 3a83a6d.jpg
    [share] => 5
    [sharedate] => 2017-10-02 )

我想将与userid相等的份额相加。 我的数组输出必须在下面。

  Array (     
        [userid] => 1042     
        [picture] => 7a86b24.jpg     
        [share] => 63    
        [sharedate] => 2017-10-02 ) 

    Array (     
         [userid] => 1007    
         [picture] => 3a83a6d.jpg     
         [share] => 159    
         [sharedate] => 2017-10-02 )

我也想在带有下面的字符串的值中使用此数组,

echo $userid;
echo $picture;
echo $share;
echo $sharedate;

我该怎么做 ? 任何想法。 谢谢。

伪代码:

make a new empty array
iterate through the existing array   {
     for every userid:
         exists in new array (check userid against index in new array)?
           yes: get share from new array and update sharecount by adding the new one to the existing one
           no: add to new array, with the userid as index...
}

code草的代码:

$newArray = []; 
foreach ($oldArray as $value) {
    $userid=$value[0];
    if (array_key_exists($userid, $newArray)){
        $objectToUpdate=$newArray['userid'];
        $objectToUpdate=array('userid'=>$value[0],'picture'=>$value[1],'share'=>$objectToUpdate[2]+$value[2],'sharedate'=>$value[3]);
    } else {
        $newArray[$userid]= array('userid'=>$value[0],'picture'=>$value[1],'share'=>$value[2],'sharedate'=>$value[3]);
    }
}

或多或少的东西...它将帮助您入门

注意事项:假设每个用户ID的日期和图片都是相同的...如果不是,则必须仔细检查如何在新数组中查看它并扩大if子句等。

这里考虑的日期和图片对于每个用户ID都是相同的。

 // define an empty array
 $new_array = array();
// loop the array
foreach($list as $value){
    $new_array[$value['userid']]['userid'] = $value['userid'];
    $new_array[$value['userid']]['picture'] = $value['picture'];
    // add the share values for each user
    $new_array[$value['userid']]['share'] = (isset($new_array[$value['userid']]['share'])) ? $new_array[$value['userid']]['share']+$value['share'] : $value['share'];
    $new_array[$value['userid']]['sharedate'] = $value['sharedate'];
}

print_r($new_array);

重置键使用array_values($ new_array);

输出:

Array
(
    [1042] => Array
        (
            [userid] => 1042
            [picture] => 7a86b24.jpg
            [share] => 63
            [sharedate] => 2017-10-02
        )

    [1007] => Array
        (
            [userid] => 1007
            [picture] => 3a83a6d.jpg
            [share] => 159
            [sharedate] => 2017-10-02
        )

)
$list = [/*your input array*/];

$intermediate = array_reduce($list, function($carry, $item) {
    $userid = $item['userid'];
    if(!isset($carry[$userid])) {
        $carry[$userid] = $item;
    } else {
        $carry[$userid]['share'] += $item['share'];
    }
    return $carry;
});

/* here we have 

Array (     
        [userid] => 1042     
        [picture] => 7a86b24.jpg     
        [share] => 63    
        [sharedate] => 2017-10-02 ) 

    Array (     
         [userid] => 1007    
         [picture] => 3a83a6d.jpg     
         [share] => 159    
         [sharedate] => 2017-10-02 )
         */

 foreach ($intermediate as $value) {
    extract($value);
    // here we have needed variables
    echo $userid;
    echo PHP_EOL; 
    echo $picture;
    echo PHP_EOL;
    echo $share;
    echo PHP_EOL;
    echo $sharedate;
    echo PHP_EOL;
 }

结果:

1042
7a86b24.jpg
63
2017-10-02
1007
3a83a6d.jpg
159
2017-10-02

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM