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
https://forums.phpfreaks.com/topic/238029-help-with-getting-unique-sub-arrays/
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;
}

?>

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!!!

 

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.