alexweber15 Posted August 21, 2009 Share Posted August 21, 2009 Hi, I'm working on a project with data that is represented as a n*n matrix. Its easy enough to do this using a multidimensional array but the catch is that I need to be able to check adjacent squares and other grid-like functionality. Again, I've managed to bypass this using arrays but its kind of annoying. So I ask: Is there some kind of grid data structure that anyone can recommend? With native implementation of stuff like checking squares above, below, etc? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/171349-grid-data-structure/ Share on other sites More sharing options...
Garethp Posted August 21, 2009 Share Posted August 21, 2009 Not that I know of. But to look against, adjascent arrays, try something like this $Array[0][0] = "Something" $Array[0][1] = "Soemething else"; $Array[0][2] = "Something else else"; $Array[1][0] = "A new Something" $Array[1][1] = "A new Soemething else"; $Array[1][2] = "A new Something else else"; $k = 0; $k2 = 1; //The indexes of the array value you should compare against those around it //Compare $Array[$k][$k2] against $Array[$k][$k2-1] //Compare $Array[$k][$k2] against $Array[$k][$k2+1] //Compare $Array[$k][$k2] against $Array[$k-1][$k2] //Compare $Array[$k][$k2] against $Array[$k+1][$k2] Quote Link to comment https://forums.phpfreaks.com/topic/171349-grid-data-structure/#findComment-903674 Share on other sites More sharing options...
alexweber15 Posted August 22, 2009 Author Share Posted August 22, 2009 Thanks for the reply Garethp, that's pretty much what I'm doing. And to avoid out of bounds errors: if(isset($array[$index])){ // compare }else{ // reached a boundary } I was hoping someone out there already had this problem and created an awesome class so I wouldn't have to do it myself! Quote Link to comment https://forums.phpfreaks.com/topic/171349-grid-data-structure/#findComment-903719 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.