views:

26

answers:

1

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);
        }
}
A: 

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>";
                }
            }
        }
    }
}
Pies
I want to compare it in the same array not the different one
Same idea, updated the code.
Pies