php总和选择了具有唯一ID的数组中的键

时间:2017-10-02 21:15:54

标签: php arrays

我有一个名为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 )

我想将分享用户ID 相等。我的数组输出必须在下面。

  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;

我该怎么办?任何的想法。谢谢。

3 个答案:

答案 0 :(得分:1)

伪代码:

python3.6/site-packages

草率代码:

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

或多或少的东西......它会让你开始

警告:假设每个用户标识的日期和图片都是相同的...如果它们不是你必须检查好你想如何在新数组中看到它并扩大if子句等

答案 1 :(得分:1)

此处考虑每个用户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);

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
        )

)

答案 2 :(得分:1)

MsgBox(stuObj.rno)
MsgBox(stuObj.name)
MsgBox(stuObj.stdsec)

结果:

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