fork download
  1. <?php
  2.  
  3. // your code goes here
  4. $arr = [
  5. [ 1, 2, 3, 4, 5],
  6. [ 6, 7, 8, 9, 10],
  7. [11, 12, 13, 14, 15],
  8. [16, 17, 18, 19, 20],
  9. ];
  10.  
  11. //echo '<pre>';
  12. foreach ($arr as $i => $a) {
  13. foreach ($a as $j => $v) {
  14. printf("I am (%d). My index is (%d, %d)\n", $v, $i, $j);
  15.  
  16. if (array_key_exists($i-1, $arr)) {
  17. printf("Top element is (%d) with index (%d, %d)\n", $arr[$i-1][$j], $i-1, $j);
  18. } else {
  19. echo "I have no top element\n";
  20. }
  21.  
  22. if (array_key_exists($j+1, $a)) {
  23. printf("Right element is (%d) with index (%d, %d)\n", $a[$j+1], $i, $j+1);
  24. } else {
  25. echo "I have no right element\n";
  26. }
  27.  
  28. if (array_key_exists($i+1, $arr)) {
  29. printf("Bottom element is (%d) with index (%d, %d)\n", $arr[$i+1][$j], $i+1, $j);
  30. } else {
  31. echo "I have no bottom element\n";
  32. }
  33.  
  34. if (array_key_exists($j-1, $a)) {
  35. printf("Left element is (%d) with index (%d, %d)\n", $a[$j-1], $i, $j-1);
  36. } else {
  37. echo "I have no Left element\n";
  38. }
  39.  
  40. echo "\n\n--------------------\n\n";
  41. }
  42. }
  43.  
Success #stdin #stdout 0.02s 24276KB
stdin
Standard input is empty
stdout
I am (1). My index is (0, 0)
I have no top element
Right element is (2) with index (0, 1)
Bottom element is (6) with index (1, 0)
I have no Left element


--------------------

I am (2). My index is (0, 1)
I have no top element
Right element is (3) with index (0, 2)
Bottom element is (7) with index (1, 1)
Left element is (1) with index (0, 0)


--------------------

I am (3). My index is (0, 2)
I have no top element
Right element is (4) with index (0, 3)
Bottom element is (8) with index (1, 2)
Left element is (2) with index (0, 1)


--------------------

I am (4). My index is (0, 3)
I have no top element
Right element is (5) with index (0, 4)
Bottom element is (9) with index (1, 3)
Left element is (3) with index (0, 2)


--------------------

I am (5). My index is (0, 4)
I have no top element
I have no right element
Bottom element is (10) with index (1, 4)
Left element is (4) with index (0, 3)


--------------------

I am (6). My index is (1, 0)
Top element is (1) with index (0, 0)
Right element is (7) with index (1, 1)
Bottom element is (11) with index (2, 0)
I have no Left element


--------------------

I am (7). My index is (1, 1)
Top element is (2) with index (0, 1)
Right element is (8) with index (1, 2)
Bottom element is (12) with index (2, 1)
Left element is (6) with index (1, 0)


--------------------

I am (8). My index is (1, 2)
Top element is (3) with index (0, 2)
Right element is (9) with index (1, 3)
Bottom element is (13) with index (2, 2)
Left element is (7) with index (1, 1)


--------------------

I am (9). My index is (1, 3)
Top element is (4) with index (0, 3)
Right element is (10) with index (1, 4)
Bottom element is (14) with index (2, 3)
Left element is (8) with index (1, 2)


--------------------

I am (10). My index is (1, 4)
Top element is (5) with index (0, 4)
I have no right element
Bottom element is (15) with index (2, 4)
Left element is (9) with index (1, 3)


--------------------

I am (11). My index is (2, 0)
Top element is (6) with index (1, 0)
Right element is (12) with index (2, 1)
Bottom element is (16) with index (3, 0)
I have no Left element


--------------------

I am (12). My index is (2, 1)
Top element is (7) with index (1, 1)
Right element is (13) with index (2, 2)
Bottom element is (17) with index (3, 1)
Left element is (11) with index (2, 0)


--------------------

I am (13). My index is (2, 2)
Top element is (8) with index (1, 2)
Right element is (14) with index (2, 3)
Bottom element is (18) with index (3, 2)
Left element is (12) with index (2, 1)


--------------------

I am (14). My index is (2, 3)
Top element is (9) with index (1, 3)
Right element is (15) with index (2, 4)
Bottom element is (19) with index (3, 3)
Left element is (13) with index (2, 2)


--------------------

I am (15). My index is (2, 4)
Top element is (10) with index (1, 4)
I have no right element
Bottom element is (20) with index (3, 4)
Left element is (14) with index (2, 3)


--------------------

I am (16). My index is (3, 0)
Top element is (11) with index (2, 0)
Right element is (17) with index (3, 1)
I have no bottom element
I have no Left element


--------------------

I am (17). My index is (3, 1)
Top element is (12) with index (2, 1)
Right element is (18) with index (3, 2)
I have no bottom element
Left element is (16) with index (3, 0)


--------------------

I am (18). My index is (3, 2)
Top element is (13) with index (2, 2)
Right element is (19) with index (3, 3)
I have no bottom element
Left element is (17) with index (3, 1)


--------------------

I am (19). My index is (3, 3)
Top element is (14) with index (2, 3)
Right element is (20) with index (3, 4)
I have no bottom element
Left element is (18) with index (3, 2)


--------------------

I am (20). My index is (3, 4)
Top element is (15) with index (2, 4)
I have no right element
I have no bottom element
Left element is (19) with index (3, 3)


--------------------