Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.