Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I have been working on this problem for a couple days now. I would like to make an array that contains a list of user id's with the sum of value1 and value2 from the list of data below.

User ID value1 Value2
1        21      2
2        2       6
3        4       43
1        3       9
2        17      8
1        12      28

Here is an amalgamation of my various attempts. It is all wrong, but conveys what I am trying to do.

$members_list = array();
if(!in_array($user_id, $members_list)){
    array_push($members_list, $user_id);
}
foreach($data_table as $data_row){
    if($user_id == $data_row['$user_id']) {
        $members_list[$_user_id]['total'] = $members_list[$user_id]['total'] + $data_row['value1'] + $data_row['value2'];
    }
}

I am trying to teach myself how to program and probably lacking a fundamental understanding of how to organise and loop through multi-dimensional arrays. Hoping someone can help me clear this up. Thanks in advance.

share|improve this question
    
Can you please post the result of var_dump($data_table)? –  Dinistro Oct 21 '14 at 13:34

3 Answers 3

up vote 0 down vote accepted

I think I would write:

$members_list = array();
foreach($data_table as $data_row){
  if(isset($members_list[$data_row['$user_id']]) == false ) {
    $members_list[$user_id] = 0;
  }
  $members_list[$_user_id]['total'] += ($data_row['value1'] + $data_row['value2']);
}

Or are you planing on adding more information about each member? in that case you could have an array instead of a number for each member.

in that case you use

 $members_list[$user_id] = array();
 $members_list[$user_id]['total'] = 0;

in the if statement.

share|improve this answer

Well, this is going to be a dummy example of what I think you're looking for...

First, here is your multi-dimensional array:

$users = array(
    array('user_id' => 1, 'value1' => 21, 'value2' => 2),
    array('user_id' => 2, 'value1' => 2, 'value2' => 6),
    array('user_id' => 3, 'value1' => 4, 'value2' => 43),
    array('user_id' => 1, 'value1' => 3, 'value2' => 9),
    array('user_id' => 2, 'value1' => 17, 'value2' => 8),
    array('user_id' => 1, 'value1' => 12, 'value2' => 28)
);

Second, loop through the list of users, grouped by user_id and adding a third key (total value) for each user:

$members_list = array();
foreach ($users as $user)
{
    if ( !array_key_exists($user['user_id'], $members_list) )
        $members_list[$user['user_id']] = $user['value1'] + $user['value2'];
    else
        $members_list[$user['user_id']] += $user['value1'] + $user['value2'];
}
share|improve this answer

So your array should look like this:

$myArray = array("userId" => array("value1", "value2"));

So I'm gonna use a few values from your list as an example:

$users = array(
    1 => array(21, 2),
    2 => array(2, 6),
    3 => array(4,43)
);

So with this array feel free to test it out with this:

foreach ($users as $user => $values) {
    echo "<b>USER:".$user."</b> -> ";
    $sum = 0;
    foreach ($values as $value) {
        echo $value.", ";
        $sum = $sum + $value;
    }
    echo " Sum: ".$sum."<br>";
}

You should get this output:

USER:1 -> 21, 2, Sum: 23
USER:2 -> 2, 6, Sum: 8
USER:3 -> 4, 43, Sum: 47

Does that help you?

share|improve this answer
    
Yes. tyvm. I understand now that you can have a key/value pair where the value is also and array or key=>value. Much appreciated. –  user2949794 Oct 21 '14 at 14:18
    
no worries. please vote up for the trouble :) –  just some guy Oct 21 '14 at 14:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.