我正在从用户那里读取大量数据,并将它们添加为关联数组,以便能够加入数据。下面给出了正确的数组值,但结果得到0。
<?php
$userID = array();
$blogusers = get_users( 'orderby=nicename&role=author' );
foreach ( $blogusers as $user ) {
$gender = 'sesso';
$single = true;
$the_user_id = $user->ID;
array_push($userID, $the_user_id);
$myUsers = $user->user_description;
$user_sex = get_user_meta( $the_user_id, $gender, $single );
$users[$user->ID] = array( "description" => $myUsers , "gender" => $user_sex );
}
$roles = array();
foreach($users as $id => $values){
$temp_roles = explode(',', $values['description']);
foreach($temp_roles as $k => $v){
$roles[trim($v)][$values['gender']][] = true;
}
}
?>
<ul class="margin-top-20">
<?php
foreach($roles as $skill => $genderB) {
$males = count($genderB['male']);
$females = count($genderB['female']);
$total = $males + $females;
echo $females;
echo "<li>We have ".$total." ".$skill." teachers, ".$males." males, ".$females." females</li>";
}
?>
</ul>
如果我0
我得到echo $females;
并且字符串全部获得0
作为值
我们有0位弗兰西老师,0位男性,0位女性
如果我在第一个foreach结束时print_r($users[$user->ID] );
,我会得到正确的结果
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 ) )
如果我在第二个foreach结束时print_r($roles);
我只获得角色值而不是性别
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 ( )
如果我在第二个foreach结束时print_r($temp_roles);
再次获得
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 )
我不明白为什么我在最后得到所有这些值的0值
答案 0 :(得分:0)
在您的数据中,性别由femmina
和maschio
定义。在您的代码中,您需要检查female
和male
。
我发现的另一个问题是你不需要男女教师。如果您打开错误报告,使用此代码即可获得
Undefined index: female
(或男性)
如果没有女性。您可以$females = isset($genderB['female']) ? count(genderB['female']): 0;
围绕它进行工作。或者通过“完整”if当然声明。