denhamd2 Posted September 1, 2013 Share Posted September 1, 2013 (edited) I have a muli-dimensional array (see below). I'm trying to use PHP to group matching events. Basically any matchname that is a duplicate (eg. Reds vs Whites) to group them together and group their linksets... Array ( [matches] => Array ( [match] => Array ( [sportname] => Football [tournamentname] => Crown League [thetime] => 201308021600 [matchname] => Reds vs Whites [linkset] => Array ( [link] => link1.html ) [0] => Array ( [sportname] => Football [tournamentname] => Prince League [thetime] => 201308021130 [matchname] => Blues vs Yellows [linkset] => Array ( [link] => link2.html ) ) [1] => Array ( [sportname] => Football [tournamentname] => Crown League [thetime] => 201308021000 [matchname] => Reds vs Whites [linkset] => Array ( [link] => link3.html ) ) ) ) ) I am trying to end up with Football: Reds vs Whites (201308021600) - Crown LeagueLinks: link1.html, link3.html Football: Blues vs Yellows (201308021130) - Prince LeagueLinks: link2.html I've been looking at the PHP manual but tearing my hair out trying to get it to output the above. Any ideas would be most welcome. Thanks Edited September 1, 2013 by denhamd2 Quote Link to comment https://forums.phpfreaks.com/topic/281747-sorting-multi-dimensional-array/ Share on other sites More sharing options...
Barand Posted September 1, 2013 Share Posted September 1, 2013 First step is to create a consistent array structure. If the data is coming from a database it is probably easier to do it in the query. Quote Link to comment https://forums.phpfreaks.com/topic/281747-sorting-multi-dimensional-array/#findComment-1447649 Share on other sites More sharing options...
denhamd2 Posted September 1, 2013 Author Share Posted September 1, 2013 HI, How would you change the array structure? Also this needs to come from an XML file rather than a DB. Quote Link to comment https://forums.phpfreaks.com/topic/281747-sorting-multi-dimensional-array/#findComment-1447657 Share on other sites More sharing options...
Barand Posted September 1, 2013 Share Posted September 1, 2013 like this [matches] => Array ( [0] => Array ( [sportname] => Football [tournamentname] => Crown League [thetime] => 201308021600 [matchname] => Reds vs Whites [linkset] => Array ( [link] => link1.html ) ) [1] => Array ( [sportname] => Football [tournamentname] => Prince League [thetime] => 201308021130 [matchname] => Blues vs Yellows [linkset] => Array ( [link] => link2.html ) ) [2] => Array ( [sportname] => Football [tournamentname] => Crown League [thetime] => 201308021000 [matchname] => Reds vs Whites [linkset] => Array ( [link] => link3.html ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/281747-sorting-multi-dimensional-array/#findComment-1447661 Share on other sites More sharing options...
Barand Posted September 1, 2013 Share Posted September 1, 2013 The matchsort() custom sort function will sort the array by sportname - tournamentname - matchname. $matches = Array ( 0 => Array ( 'sportname' => 'Football', 'tournamentname' => 'Crown League', 'thetime' => 201308021600, 'matchname' => 'Reds vs Whites', 'linkset' => Array ( 'link' => 'link1.html' ) ) , 1 => Array ( 'sportname' => 'Football', 'tournamentname' => 'Prince League', 'thetime' => 201308021130, 'matchname' => 'Blues vs Yellows', 'linkset' => Array ( 'link' => 'link2.html' ) ), 2 => Array ( 'sportname' => 'Football', 'tournamentname' => 'Crown League', 'thetime' => 201308021000, 'matchname' => 'Greys vs Whites', 'linkset' => Array ( 'link' => 'link3.html' ) ) ); usort($matches, 'matchsort'); echo '<pre>',print_r($matches, true),'</pre>'; function matchsort ($a, $b) { $x = strcmp($a['sportname'], $b['sportname']); if ($x==0) { $y = strcmp($a['tournamentname'], $b['tournamentname']); if ($y==0) { return strcmp($a['matchname'], $b['matchname']); } else{ return $y; } } else { return $x; } } OUTPUT Array ( [0] => Array ( [sportname] => Football [tournamentname] => Crown League [thetime] => 201308021000 [matchname] => Greys vs Whites [linkset] => Array ( [link] => link3.html ) ) [1] => Array ( [sportname] => Football [tournamentname] => Crown League [thetime] => 201308021600 [matchname] => Reds vs Whites [linkset] => Array ( [link] => link1.html ) ) [2] => Array ( [sportname] => Football [tournamentname] => Prince League [thetime] => 201308021130 [matchname] => Blues vs Yellows [linkset] => Array ( [link] => link2.html ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/281747-sorting-multi-dimensional-array/#findComment-1447668 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.