Oseer
Oseer

Reputation: 740

If value exists in one PHP array, add value to second array

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

Upvotes: 0

Views: 1505

Answers (2)

Levi Morrison
Levi Morrison

Reputation: 19552

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.

Upvotes: 2

Ron
Ron

Reputation: 513

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']);
}

Upvotes: 1

Related Questions