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