简体   繁体   中英

php sum selected keys in array with unique id

I have php array named print_r($list) you can see output under below;

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

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

Here considering the date and picture are same for each userid.

 // 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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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