0

I have a 2d array say, Array A[60][150] and have another array, Array B[60][150]. Now what I am trying to do is:

Given a point in Array A say x,y i want to access its neighbors to find similarity between the two elements. if they are similar then find its neighbors. So right now i am using recursive function to do it. But its throws an error saying maximum execution time has exceeded or out of memory.

So I am just wondering is there any other to solve this. I mean without using recursion.

code example:

           protected function find_neighbor($y,$x,$garray,$gr)
       {
        $r=$garray[$y][$x-1]['red'];
        if(true){
            $this->find_neighbor($y,$x-1,$garray,$gr);
        }

        $r=$garray[$y-1][$x-1]['red'];
        if(true){
            $this->find_neighbor($y-1,$x-1,$garray,$gr);
        }

        $r=$garray[$y-1][$x]['red'];
        if(true){
            $this->find_neighbor($y-1,$x,$garray,$gr);
        }

        $r=$garray[$y-1][$x+1]['red'];
        if(true){
            $this->find_neighbor($y-1,$x+1,$garray,$gr)
        }

        $r=$garray[$y][$x+1]['red'];
        if(true){
            $this->find_neighbor($y,$x+1,$garray,$gr);      
        }

        $r=$garray[$y+1][$x+1]['red'];
        if(true){
            $this->find_neighbor($y+1,$x+1,$garray,$gr);
        } 

        $r=$garray[$y+1][$x]['red'];
        if(true){
                $this->find_neighbor($y+1,$x,$garray,$gr);  
        } 

        $r=$garray[$y+1][$x-1]['red'];
        if(true){
            $this->find_neighbor($y+1,$x-1,$garray,$gr);
        }
}
1
  • Can you illustrate your example with some code? Or a few points that would fall within your example? Commented Sep 10, 2010 at 21:20

1 Answer 1

0

Untested:

$range = 3;
foreach ($A as $A1=>$tmp) {
    foreach ($tmp as $A2=>$value) {
        for ($x=0-$range; $x<=$range; $x++) {
            for ($y=0-$range; $y<=$range; $y++) {
                $F1 = $A1+$x;
                $F2 = $A2+$y;
                if (isset($A[$F1][$F2])) {
                    print "A[{$A1}][{$A2}] is close to A[{$F1}][{$F2}]<br>";
                }
            }
        }
    }
}
1
  • I want to compare it in the same array not the different one Commented Sep 10, 2010 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.