Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6677033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:04:01+00:00 2026-05-26T04:04:01+00:00

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

  • 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);
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:04:02+00:00Added an answer on May 26, 2026 at 4:04 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have converted an array to object data like this: <?php $myobject->data = (object)Array('zero','one','two');
I have two arrays. One contains id=>count and the other contains id=>name . I'm
I have a multi dimensional array in PHP. $f = array('one' => array(*doesntmatter*), two
I have two arrays in PHP. The first array ($author_array) is comprised of user_ids
I have two arrays one with OS's such as Ubuntu and Windows and another
I have two arrays in php $arrNum = array(1.7, 1.52, 0.01, 0.11); $arrStr =
I've two PHP Arrays. The first one contains a sort order. The second one
I have two arrays. The first one contains an id, username, and password from
I have a file which contains two JSON arrays; one is holding the column
I have two files, one containing an array in PHP that is echo ed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.