How to check the 4 vertical and horizontal elements in an array based on the current position?

1

Let's suppose I have an array like this:

$matrix = [[0, 1, 1, 0],
           [1, 1, 1, 0],
           [1, 0, 0, 0]];

Imagine that you are looping and capturing the position of this array as in the example below, y lines with my arrays and x the positions of each line (I do not know if this is correct):

<?php

 $data = getHierarchyMatrix(['x'=>2,'y'=>1], $matrix);

 echo '<pre>';
 print_r($data);
 function getHierarchyMatrix($currentXY, $matrix) {

    $currentX = $currentXY['x'];
    $currentY = $currentXY['y'];

    foreach ($matrix as $currentLine => $arrayLine) {

           if ($currentLine >= $currentY) {
              $previousY = ($currentLine-1);
              $nextY = ($currentLine+1);
              if (isset($matrix[$previousY])) {
                 $previousValueY = $matrix[$previousY];
              }
              if (isset($matrix[$currentLine+1])) {
                 $nextValueY = $matrix[$nextY];
              }

              foreach ($arrayLine as $current => $value) {
                  if ($current == $currentX) {
                      $currentValue = $value;
                      $previousX = ($current - 1);
                      if (isset($matrix[$current][$previousX])) {
                         $previousValueX = $matrix[$current][$previousX];
                      }
                      $nextX  = ($current + 1);
                      if (isset($matrix[$current][$nextX])) {
                         $nextValueX = $matrix[$current][$nextX];
                      }
                  }
              }
          }
      }

    return [
             'atual'    => $currentValue,
             'esquerda' => $previousValueX,
             'direita'  => $nextValueX,
             'em cima'  => $previousValueY,
             'em baixo' => $nextValueY
           ];
 }
The problem is basically taking the position x and y and the 4 elements that surround them, I do not know if I'm doing it right ... but I need to do the quadrant based on the current position, how could I do it? I do not think my method is very well worked out. Maybe there is some way to do this using some PHP function that I do not know, could anyone help me ... Its there methods that could help with this, such as:

$atual = current($array); 
$proximo = next($array);  
$anterior = prev($array); 
$ultimo = end($array);   
  

Note: I'll also need this formula for a JavaScript version

    
asked by anonymous 21.09.2015 / 21:16

2 answers

2

I imagine this will solve your problem:

function getHierarchyMatrix($currentXY, $matrix) {
    $currentX = $currentXY['x'];
    $currentY = $currentXY['y'];

    if(isset($matrix[$currentXY['y']][$currentXY['x']])){
        $atual = $matrix[$currentXY['y']][$currentXY['x']];

        $left = isset($matrix[$currentXY['y']][$currentXY['x']-1]) ? 
                $matrix[$currentXY['y']][$currentXY['x']-1] : false;
        $right = isset($matrix[$currentXY['y']][$currentXY['x']+1]) ?
                $matrix[$currentXY['y']][$currentXY['x']+1] : false;
        $top = isset($matrix[$currentXY['y']-1][$currentXY['x']]) ? 
                $matrix[$currentXY['y']-1][$currentXY['x']] : false;
        $bottom = isset($matrix[$currentXY['y']+1][$currentXY['x']]) ? 
                $matrix[$currentXY['y']+1][$currentXY['x']] : false;


        return [
            'atual'    => $atual,
            'esquerda' => $left,
            'direita'  => $right,
            'em cima'  => $top,
            'em baixo' => $bottom
            ];
    } else {
        return false;
    }

}
    
21.09.2015 / 22:20
1

The first thing you should do is validate the position of the reference element, ie:

  • If the position of the element itself is valid;
  • If the element is between two rows and two columns.

After this validation, just take the desired elements based on the position of the reference element, which we will give the name of $x :

<?php

$a = $x[x-1, y]; // elemento à esquerda
$b = $x[x+1, y]; // elemento à direita
$c = $x[x, y-1]; // elemento acima
$d = $x[x, y+1]; // elemento abaixo

For JavaScript, the logic is the same.

    
21.09.2015 / 22:14