Jump to content

"grid" data structure?


alexweber15

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/171349-grid-data-structure/
Share on other sites

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]

Link to comment
https://forums.phpfreaks.com/topic/171349-grid-data-structure/#findComment-903674
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/171349-grid-data-structure/#findComment-903719
Share on other sites

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.