0

I have a project where a user can add people to a list, that includes name, male or female, and student or leader. Each person is either male or female and a student or leader.

I am trying to get the count for female students, female leaders, male students, and male leaders but not sure how to combine them to do the count. Something like:

$femaleStudent = $female && $student;
$maleStudent = $male && $student;
$femaleLeader = $female && $leader;
$maleLeader = $male && $leader;

Then do something like this:

$total = $femaleStudent + $maleStudent + $femaleLeader + $maleLeader;

How do I combine them so I can count them?

EDIT

$female, $male, $student, and $leader are just getting the count. Here is an example (this is in WordPress):

$female = array_count_values(array_column($participants, 'participant_gender'))['Female'];

The end result I am trying to show is the total number of people and then break it down by female student, female leader, male student, and male leader. My output is basically: Female Students: 8 Male Students = 6, Female Leaders = 2, Male Leaders = 2, Total = 18

EDIT 2

Here is an example of the participants array from a var_dump:

  0 => 
    array
      'participant_first_name' => string 'Steve'
      'participant_last_name' => string 'Rogers'
      'participant_gender' => string 'Male'
      'participant_age' => string '44'
      'participant_role' => string 'Leader'
  1 => 
    array
      'participant_first_name' => string 'Lois' 
      'participant_last_name' => string 'Lane' 
      'participant_gender' => string 'Female' 
      'participant_age' => string '15' 
      'participant_role' => string 'Student'
7
  • You had better show us with an example what is contained in these variables, it may help make sense of the question Commented Jan 11, 2023 at 17:10
  • An example of the required output may also help Commented Jan 11, 2023 at 17:11
  • 1
    There's no way to get the combined counts from the separate counts. You need to loop through the original data, performing both tests. Commented Jan 11, 2023 at 17:28
  • 1
    If $female contains a number, and $student contains a number, how should these numbers (which don't have any more context) be combined by any logic? Commented Jan 11, 2023 at 18:44
  • 1
    I realize now that I need to loop through participants, I should have started with "here is my data" and then "here is what I'm trying to do". Commented Jan 11, 2023 at 20:32

1 Answer 1

1

After a bit more of research and understanding what I did wrong, I was able to find a solution:

    $femaleStudent = 0;
    $maleStudent = 0;
    $femaleLeader = 0;
    $maleLeader = 0;

    foreach($participants as $participant => $v){
        $gender = $v['participant_gender'];
        $role = $v['participant_role'];
        
        if( $gender == "Male" && $role == "Student"){
            $maleStudent++;
        }
        if($gender == "Female" && $role == "Student") {
            $femaleStudent++;
        }
        if( $gender == "Male" && $role == "Leader"){
            $maleLeader++;
        }
        if($gender == "Female" && $role == "Leader") {
            $femaleLeader++;
        }
        
    }
    $total = count($participants);
Sign up to request clarification or add additional context in comments.

Comments

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.