php - Pick 2 items with same contestId from multidimensional array -


i want pull 2 videos array of videos share same contest id. doesn't matter contest id is, long both of items share same value. might easy i'm @ 1 of points brain fried. can smell bbq. lol.

    array (     [0] => array         (             [id] => 11             [filename] => 7966727971960279-4131736221448300-adele_set_fire_to_the_rain_cover_by_sara_niemietz_             [added] => 2013-05-06 08:50:34             [userid] => 14             [contestid] => 3             [complete] => 1         )      [1] => array         (             [id] => 15             [filename] => 7090052934711575-3964279645504194-adele_set_fire_to_the_rain_cover_by_sabrina             [added] => 2013-05-06 09:58:08             [userid] => 14             [contestid] => 4             [complete] => 1         )      [2] => array         (             [id] => 14             [filename] => 6959153088629946-5656902896285110-adele_set_fire_to_the_rain_cover_by_public_school_kids_own_with_guitar             [added] => 2013-05-06 09:42:10             [userid] => 14             [contestid] => 4             [complete] => 1         )      [3] => array         (             [id] => 10             [filename] => 2795880037180269-1520875971490576-test             [added] => 2013-05-06 08:02:53             [userid] => 9             [contestid] => 3             [complete] => 1         )      [4] => array         (             [id] => 12             [filename] => 6683752765687164-9656171421456902-adele_set_fire_to_the_rain_arlene_zelina_cover_             [added] => 2013-05-06 09:21:12             [userid] => 11             [contestid] => 3             [complete] => 1         )      [5] => array         (             [id] => 13             [filename] => 6917133659214044-8729839283435145-adele_set_fire_to_the_rain_boyce_avenue_cover_on_itunes_spotify             [added] => 2013-05-06 09:34:50             [userid] => 12             [contestid] => 3             [complete] => 1         )  ) 

you store them in array each element being subarray elements sharing same contestid:

$sorted = array();  foreach ($elements $e) {     $sorted[$e['contestid']][] = $e; }  print_r($sorted); 

edit

this function give array 2 elements same contestid, or null if not found. if don't need them random, remove shuffle() call.

function getelementsfromarray($elements, $numberofelements = 2) {     shuffle($elements); // in case if want different elements in each call     $sorted = array();      foreach ($elements $e) {         $contestid = $e['contestid'];         $sorted[$contestid][] = $e;          if (count($sorted[$contestid]) == $numberofelements)             return $sorted[$contestid];     }      return null;         } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -