简体   繁体   English

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

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

I have php array named print_r($list) you can see output under below; 我有一个名为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 )

I want to sum share with userid equal ones. 我想将与userid相等的份额相加。 my array output must be under below. 我的数组输出必须在下面。

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

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

Also I want to use this array inside values with string like under below, 我也想在带有下面的字符串的值中使用此数组,

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

How can I do it ? 我该怎么做 ? any idea. 任何想法。 Thank you. 谢谢。

pseudocode: 伪代码:

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

sloppy code: 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]);
    }
}

something like that more or less... it will get you started 或多或少的东西...它将帮助您入门

caveats: it is assumed that the dates and pictures for every userid are the same... if they're not you have to check well how do you want to see it in the new array and broaden the if clause, etc. 注意事项:假设每个用户ID的日期和图片都是相同的...如果不是,则必须仔细检查如何在新数组中查看它并扩大if子句等。

Here considering the date and picture are same for each userid. 这里考虑的日期和图片对于每个用户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);

to reset the keys use array_values($new_array); 重置键使用array_values($ new_array);

Out put: 输出:

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

Result: 结果:

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