0

I have the following code in PHP

foreach ($events as $event)
{
    $event = $event->organiser;
    var_dump($event);
}

Then i am getting output like for $event

array
 0 => string '40138' (length=5)
 1 => string '40137' (length=5)
array
 0 => string '40140' (length=5)
 1 => string '40138' (length=5)

Next i am creating a loop to get values as per the above ID (40137,40138) in to one array, here i want to create a unique array. How to do that ?

So as per the unique ID i can create the following 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>";
        }
    }
}

So how to get the unique array combining of multiple array ?

2
  • Use array_filter function. Commented Feb 27, 2014 at 10:37
  • I tried that but its didn't worked for me..i tried like $event = $event->organiser; $counts = array_filter($event, create_function('$x', 'return $x > 1;')); var_dump($counts); Commented Feb 27, 2014 at 10:41

2 Answers 2

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>";
}

For php under 5.3 (when lamba-functions weren't supported):

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>";
}
Sign up to request clarification or add additional context in comments.

3 Comments

Its showing error. Its saying unexpected function at $organisers = array_reduce($events, function($ids, $el){. what to do ? is it supported by PHP 5.3 version
looks like you have old php. Try the 2nd solution.
Thanks dude its working perfectly. Thanks for ur tym & effort
1

Not sure if this is what you looking for, but the below code makes sure your ID is not printed twice:

$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
            }
        }
    }

4 Comments

@RIADev If I'm correct, your array has repeating IDs and you want to display only unique ones, right?
In the above code, the array $rec_values will have all the unique values. What problem are you facing with the code? Maybe you should do a print_r($organiser); inside the foreach and add what it prints to your question.
Its printing "4013740138" only. Its missing the 40140
Since I just saw your edited post of the first foreach loop, add the $rec_values = array(); before the first foreach and try.

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.