fork(2) download
  1. <?php
  2.  
  3. function diagonalSum($arr) {
  4. $i = 0;
  5. $n = count($arr);
  6. return array_reduce($arr,
  7. function ($c, $str) use (&$i, $n ) {
  8. $i++;
  9. return $c + $str[$i-1] + $str[$n-$i];
  10. }, 0);
  11. }
  12.  
  13. print(diagonalSum(
  14. [
  15. [1, 2, 3, 4],
  16. [5, 6, 7, 8],
  17. [9, 10, 11, 12],
  18. [13, 14, 15, 16],
  19. ])); // should be 68
Success #stdin #stdout 0s 82560KB
stdin
Standard input is empty
stdout
68