Jump to content

help with getting unique sub arrays


blackhawk

Recommended Posts

what is the easiest way to get unqiue sub arrays based on date from within my larger array?  here is what i mean in detail.  I have an array that outputs this...

 


[0] => Array
        (
            [date] => 2010-11-02
            [time] => 7:00 p.m.
            [location] => Illinois
            [team1] => 
            [team1sc] => 
            [team2] => 
            [team2sc] => 
            [winner] => Illinois 
        )

    [1] => Array
        (
            [date] => 2010-11-02
            [time] => 7:15 p.m.
            [location] => Purdue
            [team1] => University of Indianapolis
            [team1sc] => 59
            [team2] => Purdue
            [team2sc] => 82
            [winner] => Purdue
        )

    [2] => Array
        (
            [date] => 2010-11-06
            [time] => 3:05 p.m.
            [location] => Southern Illinois
            [team1] => University of Indianapolis
            [team1sc] => 58
            [team2] => Southern Illinois
            [team2sc] => 65
            [winner] => Southern Illinois
        )

 

I don't need value [ 1 ] because value [ 0 ] already has the same date!  My goal is to get a list of all the unique [date]s from this entire multidemonsional array. (FYI - I know I will be loosing blocks of arrays that have duplicate dates - THATS OK).  But it's a bit of a struggle for me unique array sections out of this...any help or advice would be great!

 

thanks!

 

 

 

 

 

Link to comment
Share on other sites

Something like this function

 

<?php

$array = array(
array('date' => '5-10','other' => 'somedata'),
array('date' => '5-10','other' => 'somedata'),
array('date' => '5-11','other' => 'somedata'),
array('date' => '5-12','other' => 'somedata'),
array('date' => '5-10','other' => 'somedata'),
array('date' => '5-12','other' => 'somedata')
);

$array = multiuniq( $array, 'date' );

print_r($array);

function multiuniq( array $array, $ukey ) {
$r = array();
$e = array();
foreach( $array as $arr ) {
	if( in_array($arr[$ukey],$e) )
		continue;
	$r[] = $arr;
	$e[] = $arr[$ukey];
}
return $r;
}

?>

Link to comment
Share on other sites

thank you so much it works!! if you don't mind educating me for a quick sec. what is roles of....

 

1.

if( in_array($arr[$ukey],$e) )
		continue;
	$r[] = $arr;
	$e[] = $arr[$ukey];

 

what is the continue doing?... it looks like $ e is not in the $ array initially...

 

 

thanks again!!!

 

 

Link to comment
Share on other sites

http://php.net/manual/en/control-structures.continue.php

 

It pretty much skips anything below it in the curly braces, and starts the loop over. Alternately, you could do something like

 

function multiuniq( array $array, $ukey ) {
$r = array();
$e = array();
foreach( $array as $arr ) {
	if( !in_array($arr[$ukey],$e) ) {
		$r[] = $arr;
		$e[] = $arr[$ukey];
	}
}
return $r;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.