Thread: help for (for)
View Single Post
Old Mar 28th, 2008, 12:07 PM   #3
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 403
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Re: help for (for)

This is a bit more memory intensive and slower as well, but I like how it works:
php Syntax (Toggle Plain Text)
  1. <?php
  2. // Get matrix size
  3. $rowSize = $_POST['rw'];
  4. $colSize = $_POST['col'];
  5. ?>
  6. <?php
  7. // Construct matrix
  8. $shiftable = array();
  9. for($i = 0; $i < $rowSize; $i++){
  10. $temp = array();
  11. for($j = 0; $j < $colSize; $j++){
  12. array_push($temp, $j);
  13. }
  14. array_push($shiftable, $temp);
  15. shiftRow($shiftable, $i, $i); // Shift matrix accordingly
  16. }
  17. ?>
  18. <pre>
  19. <?php
  20. // Print matrix
  21. foreach($shiftable as $row){
  22. foreach($row as $j => $value){
  23. echo str_pad($value, 2, ' ', STR_PAD_LEFT);
  24. }
  25. echo "\n";
  26. }
  27. ?>
  28. </pre>
  29. <?php
  30. // Row shifting function
  31. function shiftRow(&$dArray, $shiftRow, $shiftAmount){
  32. for($i = 0; $i < $shiftAmount; $i++){
  33. array_push($dArray[$shiftRow], array_shift($dArray[$shiftRow]));
  34. }
  35. }
  36. ?>
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote