我有一个教师名单,其中包含以下属性:
teacher 1 skills = English, Math
teacher 1 gender = Male
teacher 2 skills = Spanish, Math, Law
teacher 1 gender = sex = Female
teacher 3 skills = English, Geography
teacher 3 gender = Male
首先,我想告诉我们有多少英语(和其他任何事情)的老师,因为技能可以是多种多样的,我所做的就是检查数组中的副本并计算它:
$stack = array();
$blogusers = get_users( 'orderby=nicename&role=author' );
foreach ( $blogusers as $user ) {
$myUsers = $user->user_description (this is the skill list);
$mystrings = strtolower($user->user_description);
$descTokens = explode(',', $mystrings);
$descTokens = array_map('trim',$descTokens);
$stack = array_merge($stack, $descTokens);
}
$count_values = array();
foreach ($stack as $a) {
@$count_values[$a]++;
}
$total_duplicates = 0;
foreach ($count_values as $a) {
if($count_values[$a]<=1){
unset($count_values[$a]);
} else{
$total_duplicates += $count_values[$a];
}
}
然后这样做
<ul class="margin-top-20">
<?php
foreach ($count_values as $key=>$count ) {
echo '<li>'.$count.' di <strong>'.$key.'</li>';
}
?>
</ul>
这是正确的,输出是正确的:
2 English teachers
2 Math teachers
1 Spanish
1 Law
1 Geography
但我也有性别,所以理想情况下我想拥有
2 English teachers, 1 male, 1 male
2 Math teachers, 1 male, 1 female
1 Spanish, 1 female
1 Law, 1 female
1 Geography, 1 male
我尝试添加:
$sex = array();
$user_sex = get_user_meta( $the_user_id, $gender, $single );
array_push($sex, $user_sex);
foreach($descTokens as $lang){
$userSex[$lang] += $user_sex;
}
foreach ($count_values as $key=>$count ) {
echo '<li>'.$count.' di <strong>'.$key.' gender '. $userSex[$key] .'</li>';
}
但那不对。
Fullcode我正在尝试:
<?php
$stack = array();
$userID = array();
$nPost = array();
$listmaterie = array();
$materie = array();
$gender = 'sesso';
$single = true;
$sex = array();
$blogusers = get_users( 'orderby=nicename&role=author' );
$userSex = array();
foreach ( $blogusers as $user ) {
$myUsers = $user->user_description;
$mystrings = strtolower($user->user_description);
$descTokens = explode(',', $mystrings);
$descTokens = array_map('trim',$descTokens);
$stack = array_merge($stack, $descTokens);
$the_user_id = $user->ID;
$numPosts = count_user_posts( $the_user_id );
array_push($userID, $the_user_id);
array_push($nPost, $numPosts);
$user_sex = get_user_meta( $the_user_id, $gender, $single );
array_push($sex, $user_sex);
foreach($descTokens as $lang){
$userSex[$lang] += $user_sex;
}
}
// get the count for each language by counting the duplicate strings;
$count_values = array();
foreach ($stack as $a) {
@$count_values[$a]++;
}
$total_duplicates = 0;
foreach ($count_values as $a) {
if($count_values[$a]<=1){
unset($count_values[$a]);
} else{
$total_duplicates += $count_values[$a];
}
}
$count_sex = array();
foreach ($sex as $b) {
@$count_sex[$b]++;
}
$total_duplicates_sex = 0;
foreach ($count_sex as $b) {
if($count_sex[$b]<=1){
unset($count_sex[$b]);
} else{
$total_duplicates_sex += $count_sex[$b];
}
}
?>
<h3 class="margin-top-40">Numero complessivo docenti su Jikū:</h3>
<ul class="margin-top-20 margin-bottom-20">
<?php
$totalInsegnanti = count($userID);
echo '<li>'. $totalInsegnanti .'</li>';
?>
</ul>
<h3>Totale insegnanti per materia su Jikū:</h3>
<ul class="margin-top-20">
<?php
foreach ($count_values as $key=>$count ) {
echo '<li>'.$count.' di <strong>'.$key.' gender '. $userSex[$key] .'</li>';
}
?>
</ul>
<h3>Totale insegnanti sesso su Jikū:</h3>
<ul class="margin-top-20">
<?php
foreach ($count_sex as $key=>$count_sex) {
echo '<li>'.$count_sex.' <strong>'.$key.'</strong></li>';
}
?>
</ul>
转储 我有3个用户
var_dump($stack);
array(11){[0] =&gt; string(8)&#34; francese&#34; [1] =&GT; string(7)&#34; chimica&#34; [2] =&GT; string(6)&#34; fisica&#34; [3] =&GT; string(7)&#34; scienze&#34; [4] =&GT; string(7)&#34; inglese&#34; [5] =&GT; string(6)&#34; fisica&#34; [6] =&GT; string(7)&#34; chimica&#34; [7] =&GT;串(8) &#34; spagnolo&#34; [8] =&GT; string(8)&#34; francese&#34; [9] =&GT; string(6)&#34; fisica&#34; [10] =&GT; string(8)&#34; italiano&#34; }
var_dump($sex);
array(3){[0] =&gt; string(7)&#34; maschio&#34; [1] =&GT; string(7)&#34; maschio&#34; [2] =&GT; string(7)&#34; femmina&#34; }
答案 0 :(得分:0)
代码:
foreach($blogusers as $user){
foreach(explode(', ',$user->user_description) as $skill){ //"comma then space" delimited
$result[$skill][]=get_user_meta($user->ID,'sesso','true'); // push user's gender into subject's array
}
}
foreach($result as $subject=>$gender_array){
echo "<div>",sizeof($gender_array)," $subject teachers";
$count_genders=array_count_values($gender_array); // avoids php NOTICE @ array_walk
array_walk($count_genders,function($v,$k){echo ", $v $k",($v!=1?"s":"");});
echo "</div>";
}
考虑到您的示例输入数据,我的方法将输出:
2 English teachers, 2 Males
2 Math teachers, 1 Male, 1 Female
1 Spanish teachers, 1 Female
1 Law teachers, 1 Female
1 Geography teachers, 1 Male