0

I have two PHP arrays. One contains a group name and another contains a pay wage value.

$group_wages_array = Array ( [0] => 1 [1] => 4 [2] => 1 [3] => 3 );

This means there are four employees on the schedule. Two are assigned to group 1, another to group 4 and the last to group 3.

The second array is as follows:

$tot_wages_array = Array ( [0] => 500 [1] => 44 [2] => 80 [3] => 11.25 );

This is a sample array of each employee's wage. Both arrays are constructed in order as values are added in a mysql while loop as it pulls the info from the database.


Later on down the line, I combine the two arrays to get one array where the key is the group number and the value is the total wages for that group:

$combined_group_wages = array_combine($group_wages_array, $tot_wages_array);

This works like a charm EXCEPT for when more than one employee is assigned to the same group. These arrays are built in a mysql while loop as it loops through each employee's info:

array_push($tot_wages_array, $totemp_wages_sch); // Add their wage to the array
array_push($group_wages_array, $emp_data['group_id']); // Add their group to the array

Instead of just pushing the data to the array, I need to do this... I know the english but I don't know how to code it:

If $emp_data['group_id'] exists as value in $group_wages_array, add nothing to this array but get the key. Add $totemp_wages_sch to $tot_wages_array where key = group_wages_array key

I know it sounds more like an SQL query but I have to keep the keys and values in order so that they can be combined later in the page. If I can get this to work right, The arrays shown in the example would be:

$group_wages_array = Array ( [0] => 1 [1] => 4 [2] => 3 );
$tot_wages_array = Array ( [0] => 580 [1] => 44 [2] => 11.25 );

$combined_group_wages = array_combine($group_wages_array, $tot_wages_array);

$combined_group_wages = Array ( [1] => 580 [4] => 44 [3] => 11.25 );

...I've got to make this work using PHP. Any ideas?


I came up with a solution based on a combination of two of the answers submitted below. Here it is if it can help someone:

        if(in_array($emp_data['group_id'], $group_wages_array)){
        $key = key($group_wages_array); 
        $tot_wages_array[$key] += $totemp_wages_sch;

        } else {

        array_push($group_wages_array, $emp_data['group_id']);      
        array_push($tot_wages_array, $totemp_wages_sch);
        }
6
  • In your last example code block, show your desired $combined_group_wages array. Commented Oct 12, 2011 at 22:35
  • Updated showing the new combined array. Commented Oct 12, 2011 at 22:38
  • I would suggest that you deal with objects, not these array abstractions. I'll look over your code and try to help, but that's honestly what I would suggest to you. Commented Oct 12, 2011 at 22:46
  • @user971230: I mean show us your desired $combined_group_wages array when there is more than one employee in the same group. Show us what you wish the resulting array would look like in that case. Commented Oct 12, 2011 at 22:48
  • Whatever the best solution would be. I get a little lost in objects, that's why I'm using these arrays. Like I said, it works as long as all I need to do is "push" the data to the array and no groups are ever repeated. Commented Oct 12, 2011 at 22:49

2 Answers 2

2

This should do it:

$group_wages_array = array(1, 4, 1, 3);
$tot_wages_array = array(500, 44, 80, 11.25);

$combined_group_wages = array();

for ($i=0; $i<count($group_wages_array); $i++) {
    $group = $group_wages_array[$i];
    if (array_key_exists($group_wages_array[$group], $combined_group_wages)) {
        $combined_group_wages[$group] += $tot_wages_array[$i];
    } else {
        $combined_group_wages[$group] = $tot_wages_array[$i];
    }

}

print_r($combined_group_wages);

Yields:

Array
(
    [1] => 580
    [4] => 44
    [3] => 11.25
)

But I recommend that you just switch to using objects to better represent your data.

Sign up to request clarification or add additional context in comments.

3 Comments

Man that looks nice but when I print_r($combined_group_wages) all I get is array()
@user971230 That is the exact code I ran my tests on. You must have done something wrong when you copied or implemented it.
I came up with a solution based on a combination of your code and theprestig3's above. I'm posting it now. Thank you for your help!
1

If I could see the entirety of the code that would help a lot, but here's your English converted to php. Show me more code and I can perfect it, until then try this ->

if(in_array($emp_data['group_id'], $group_wages_array)){
    $key = key($group_wages_array);
    $tot_wages_array[$key] = $totemp_wages_sch;
} else {
    array_push($group_wages_array, $emp_data['group_id']);
}

1 Comment

Yes I agree more info would help. Also at least half of it is an sql thing and a single (multidimensinal maybe) array could be built inside the while loop right away! Posting a table's description would help for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.