0

I have 1 field for each users which gives me a series of skills:

$user->user_description

User 1 = Geography, English, Math, Science

User 2 = English, Math, Geography

User 3 = French, Math

NOTE Each one is a phrase with comma separated strings

Then I have another field for the gender which for each user I get:

get_user_meta( $the_user_id, $gender, $single ); 

User 1 = Male

User 2 = Female

User 3 = Male

The output I am looking for is:

  • We have 2 Math teachers with gender male
  • We have 2 Geography teachers with gender male and female
  • We have 1 French teacher with gender female
  • We have 2 English teacher with gender male and female
  • We have 1 Science teacher with gender male

This is how I loop the users:

$blogusers = get_users( 'orderby=nicename&role=author' );
  foreach ( $blogusers as $user ) {

NOTE

I tried to simplify on here but I went much further up with the code which I have it in another question. Any other info you may need I am ready to provide it, it is really getting me stack. The main bit which blocks me is how to associate the Gender together with each skills once these have been splitted and counted.

What I have managed to do:

  1. Split the comma separated list and push them into an array
  2. Check for duplicated words in the array in order to be able to achieve the number of each skill e.g. 2 English
  3. Be able to have the tot. number of each gender

But yet, not be able to add the gender for each skill once splitted

Community
  • 1
  • 1
Roberto Marras
  • 153
  • 2
  • 11
  • Do you want something like Array [userID/0] -> ([gender]=>male , [role]=>Math) – clearshot66 Apr 11 '17 at 17:35
  • @clearshot66 yeah just make sure that each user have a set of skills and these could be or could be not shared, and for each skills we still have to remember the gender it had – Roberto Marras Apr 11 '17 at 17:37

2 Answers2

0

Just an example, assuming each user has these variables holding the value. $i is used to loop each position for the new array. I'm not sure how you're looping users entirely, usually i do this within a while($row=$result->fetch_assoc) type situation.

$blogusers = get_users( 'orderby=nicename&role=author' );
  for($i=0;$i<count($blogusers);$i++){   // run through your list of individuals
$userAttribList[$i]['gender'] = $gender;   // Assign the position 0 attribute
$userAttribList[$i]['role']   = $role;    /// assigb pos  attribute
$userAttribList[$i]['skill']  = $skill;  // assign pos 0 attribute

}
echo "<xmp>";
print_r($userAttribList);
echo "</xmp>";

will print out like this userAttribList = array( [0] => (['gender']=>male,['role']=>male,['skill']=>skiiing));

Just an example, not actual data but you get the idea

Loop and use like this

foreach($userAttribList as $user){
 echo $user['gender'];
 echo $user['skill'];
 echo $user['role'];
}
clearshot66
  • 2,292
  • 1
  • 8
  • 17
  • we're are not splitting the skill list in the phrase tho right? This is assuming we have a user for each skill with a gender, am i right? – Roberto Marras Apr 11 '17 at 18:03
  • Nothing is split. Think of this as like, THIS PERSON has THESE ATTRIBUTES. If thinking of a database column/excel columns, the gender, skill, role are column headers, and $i is the row ID or "person"; Your description isn't entirely clear, but if you only need gender and role (role as in math, english etc), you can omit the skill adding, I just put it as a third attribute to show you that you can do it that way – clearshot66 Apr 11 '17 at 18:04
  • Because then you can loop that array and count the user's with those specific attributes and generate counts – clearshot66 Apr 11 '17 at 18:07
  • I do need the split tho, because i need to achieve **We have 2 Math teachers with gender male** therefore i need to first split the skill list as they are a phrase, then count how many of each we have in the array, i do so by counting duplicates, finally for each skill get the gender – Roberto Marras Apr 11 '17 at 18:11
  • How is the data coming in, exactly? – clearshot66 Apr 11 '17 at 18:18
  • check this other SO question i did, there is the full code and data and the var dumps http://stackoverflow.com/questions/43347145/how-to-add-associative-array – Roberto Marras Apr 11 '17 at 18:21
0

If you can establish a combined array that looks roughly like this:

$users[] = array('description'=>'Geography, English, Math, Science', 'gender' => 'male');
$users[] = array('description'=>'English, Math, Geography', 'gender' => 'female');
$users[] = array('description'=>'French, Math', 'gender' => 'male');

The following:

$roles = array();

foreach($users as $id => $values){
    $temp_roles = explode(',', $values['description']);
    foreach($temp_roles as $k => $v){
      $roles[trim($v)][$values['gender']][] = true;
    }
}
foreach($roles as $skill => $gender){
    $males = count($gender['male']);
    $females = count($gender['female']);

    $total = $males + $females;

    echo "We have {$total} {$skill} teachers, {$males} males, {$females} females <br>";

}

Will yield (formatting is up to you):

We have 2 Geography teachers, 1 males, 1 females
We have 2 English teachers, 1 males, 1 females
We have 3 Math teachers, 2 males, 1 females
We have 1 Science teachers, 1 males, 0 females
We have 1 French teachers, 1 males, 0 females

Dano
  • 169
  • 9
  • ok this looks promising but nope, I am not sure how to establish a combined array of those. Taken the fact I get each skill phrase looping user and grabbing `$user->user_description` and to get each user gender i loop and do this `$gender = get_user_meta( $the_user_id, $gender, $single );` How would I merge the 2 returns into an associative array? ($user[] would be an associative array right?) – Roberto Marras Apr 11 '17 at 18:30
  • maybe i could use array_merge ? – Roberto Marras Apr 11 '17 at 18:38
  • `foreach ( $blogusers as $user ) { $users[$user->ID] = array( "description" => $user->user_description , "gender" => get_user_meta( $user->ID, $gender, $single ) ); }` – Dano Apr 11 '17 at 18:46
  • adding that at the top, results in We have 0 francese teachers, 0 males, 0 females. First of all i think we are not counting the roles. Which i was doing it by comparing duplicate role string once added into the array – Roberto Marras Apr 11 '17 at 18:56
  • if I do `print_r($users[$user->ID] );` right at the end of the first foreach, i get the right result tho: `Array ( [description] => francese, chimica, fisica, scienze [gender] => Array ( [0] => maschio ) ) Array ( [description] => inglese, fisica, chimica, spagnolo [gender] => Array ( [0] => maschio ) ) Array ( [description] => francese, fisica, italiano [gender] => Array ( [0] => femmina ) )` yet We have 0 chimica teachers, 0 males, 0 females – Roberto Marras Apr 11 '17 at 19:04
  • then if I do `print_r($roles);` at the end of the second foreach i get only the roles values but not the gender: `Array ( [francese] => Array ( ) [chimica] => Array ( ) [fisica] => Array ( ) [scienze] => Array ( ) ) Array ( [francese] => Array ( ) [chimica] => Array ( ) [fisica] => Array ( ) [scienze] => Array ( ) [inglese] => Array ( ) [spagnolo] => Array ( ) ) Array ( [francese] => Array ( ) [chimica] => Array ( ) [fisica] => Array ( ) [scienze] => Array ( ) [inglese] => Array ( ) [spagnolo] => Array ( ) [italiano] => Array ( ) ` – Roberto Marras Apr 11 '17 at 19:07
  • and if i do `print_r($temp_roles);` at the end of the second foreach i get `Array ( [0] => francese [1] => chimica [2] => fisica [3] => scienze ) Array ( [0] => inglese [1] => fisica [2] => chimica [3] => spagnolo ) Array ( [0] => francese [1] => fisica [2] => italiano ) ` – Roberto Marras Apr 11 '17 at 19:11