blackhawk Posted May 31, 2011 Share Posted May 31, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/238029-help-with-getting-unique-sub-arrays/ Share on other sites More sharing options...
xyph Posted May 31, 2011 Share Posted May 31, 2011 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/238029-help-with-getting-unique-sub-arrays/#findComment-1223143 Share on other sites More sharing options...
blackhawk Posted May 31, 2011 Author Share Posted May 31, 2011 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!!! Quote Link to comment https://forums.phpfreaks.com/topic/238029-help-with-getting-unique-sub-arrays/#findComment-1223150 Share on other sites More sharing options...
xyph Posted May 31, 2011 Share Posted May 31, 2011 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; } Quote Link to comment https://forums.phpfreaks.com/topic/238029-help-with-getting-unique-sub-arrays/#findComment-1223153 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.