我在PHP中有以下代码
foreach ($events as $event)
{
$event = $event->organiser;
var_dump($event);
}
然后我得到的输出就像$ event
一样array
0 => string '40138' (length=5)
1 => string '40137' (length=5)
array
0 => string '40140' (length=5)
1 => string '40138' (length=5)
接下来我创建一个循环来按照上面的ID(40137,40138)获取值到一个数组,这里我想创建一个唯一的数组。怎么做?
因此,根据唯一ID,我可以创建以下UI
foreach ($events as $event)
{
if(!empty($event->organiser)){
if(is_array($event->organiser)){
foreach ($event->organiser as $organiser){
$mentors_entities = get_entity($organiser);
$name = $mentors_entities->title;
$img = $vars['url'].'mod/mentor/icon.php?mentorguid='.$organiser;
$body .= "<div class='event_mntdes'><div class='event_mntimg'><img src=".$img.'&size=large'."></div><div class='event_mntcontent'><b>".$name."</b><br/>".elgg_get_excerpt($mentors_entities->mentorshrt,'150')."</div></div>";
}
}else{
$organiser = $event->organiser;
$mentors_entities = get_entity($organiser);
$name = $mentors_entities->title;
$img = $vars['url'].'mod/mentor/icon.php?mentorguid='.$organiser;
$body .= "<div class='event_mntdes'><div class='event_mntimg'><img src=".$img.'&size=large'."></div><div class='event_mntcontent'><b>".$name."</b><br/>".elgg_get_excerpt($mentors_entities->mentorshrt,'150')."</div></div>";
}
}
}
那么如何获得多个数组的唯一数组?
答案 0 :(得分:1)
不确定这是否是您要找的,但以下代码确保您的ID不会打印两次:
$rec_values = array(); // will store the array values
if(!empty($event->organiser)){
if(is_array($event->organiser)){
foreach ($event->organiser as $organiser){
//checks if ID has been entered before
//if not, display it
if(!in_array($organiser,$rec_values)){
$mentors_entities = get_entity($organiser);
$name = $mentors_entities->title;
$img = $vars['url'].'mod/mentor/icon.php?mentorguid='.$organiser;
$body .= "<div class='event_mntdes'><div class='event_mntimg'><img src=".$img.'&size=large'."></div><div class='event_mntcontent'><b>".$name."</b><br/>".elgg_get_excerpt($mentors_entities->mentorshrt,'150')."</div></div>";
$rec_values[] =$organiser; //record the ID
}
}
}else{
//same for the else part
if(!in_array($organiser,$rec_values)){
$organiser = $event->organiser;
$mentors_entities = get_entity($organiser);
$name = $mentors_entities->title;
$img = $vars['url'].'mod/mentor/icon.php?mentorguid='.$organiser;
$body .= "<div class='event_mntdes'><div class='event_mntimg'><img src=".$img.'&size=large'."></div><div class='event_mntcontent'><b>".$name."</b><br/>".elgg_get_excerpt($mentors_entities->mentorshrt,'150')."</div></div>";
$rec_values[] =$organiser; //record the ID
}
}
}
答案 1 :(得分:1)
$organisers = array_reduce($events, function($ids, $el){ //lets collect all the ids in one array
if(is_array($el->organiser)){
$ids = array_merge($ids, $el->organiser);
}else{
$ids[] = $el->organiser;
}
return $ids;
}, array());
$orgsUnique = array_unique($organisers); //leave only uniqie ids
foreach ($orgsUnique as $organiser)
{
//do what you need...
$mentors_entities = get_entity($organiser);
$name = $mentors_entities->title;
$img = $vars['url'].'mod/mentor/icon.php?mentorguid='.$organiser;
$body .= "<div class='event_mntdes'><div class='event_mntimg'><img src=".$img.'&size=large'."></div><div class='event_mntcontent'><b>".$name."</b><br/>".elgg_get_excerpt($mentors_entities->mentorshrt,'150')."</div></div>";
}
对于5.3下的php(当不支持lamba函数时):
function reduceFunc($ids, $el){
if(is_array($el->organiser)){
$ids = array_merge($ids, $el->organiser);
}else{
$ids[] = $el->organiser;
}
return $ids;
}
$organisers = array_reduce($events, 'reduceFunc', array()); //lets collect all the ids in one array
$orgsUnique = array_unique($organisers); //leave only uniqie ids
foreach ($orgsUnique as $organiser)
{
//do what you need...
$mentors_entities = get_entity($organiser);
$name = $mentors_entities->title;
$img = $vars['url'].'mod/mentor/icon.php?mentorguid='.$organiser;
$body .= "<div class='event_mntdes'><div class='event_mntimg'><img src=".$img.'&size=large'."></div><div class='event_mntcontent'><b>".$name."</b><br/>".elgg_get_excerpt($mentors_entities->mentorshrt,'150')."</div></div>";
}