matrix.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /*
  3. Copyright (c) 2009-2017 F3::Factory/Bong Cosca, All rights reserved.
  4. This file is part of the Fat-Free Framework (http://fatfreeframework.com).
  5. This is free software: you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the Free Software
  7. Foundation, either version 3 of the License, or later.
  8. Fat-Free Framework is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with Fat-Free Framework. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. //! Generic array utilities
  16. class Matrix extends Prefab {
  17. /**
  18. * Retrieve values from a specified column of a multi-dimensional
  19. * array variable
  20. * @return array
  21. * @param $var array
  22. * @param $col mixed
  23. **/
  24. function pick(array $var,$col) {
  25. return array_map(
  26. function($row) use($col) {
  27. return $row[$col];
  28. },
  29. $var
  30. );
  31. }
  32. /**
  33. * Rotate a two-dimensional array variable
  34. * @return NULL
  35. * @param $var array
  36. **/
  37. function transpose(array &$var) {
  38. $out=[];
  39. foreach ($var as $keyx=>$cols)
  40. foreach ($cols as $keyy=>$valy)
  41. $out[$keyy][$keyx]=$valy;
  42. $var=$out;
  43. }
  44. /**
  45. * Sort a multi-dimensional array variable on a specified column
  46. * @return bool
  47. * @param $var array
  48. * @param $col mixed
  49. * @param $order int
  50. **/
  51. function sort(array &$var,$col,$order=SORT_ASC) {
  52. uasort(
  53. $var,
  54. function($val1,$val2) use($col,$order) {
  55. list($v1,$v2)=[$val1[$col],$val2[$col]];
  56. $out=is_numeric($v1) && is_numeric($v2)?
  57. Base::instance()->sign($v1-$v2):strcmp($v1,$v2);
  58. if ($order==SORT_DESC)
  59. $out=-$out;
  60. return $out;
  61. }
  62. );
  63. $var=array_values($var);
  64. }
  65. /**
  66. * Change the key of a two-dimensional array element
  67. * @return NULL
  68. * @param $var array
  69. * @param $old string
  70. * @param $new string
  71. **/
  72. function changekey(array &$var,$old,$new) {
  73. $keys=array_keys($var);
  74. $vals=array_values($var);
  75. $keys[array_search($old,$keys)]=$new;
  76. $var=array_combine($keys,$vals);
  77. }
  78. /**
  79. * Return month calendar of specified date, with optional setting for
  80. * first day of week (0 for Sunday)
  81. * @return array
  82. * @param $date string
  83. * @param $first int
  84. **/
  85. function calendar($date='now',$first=0) {
  86. $out=FALSE;
  87. if (extension_loaded('calendar')) {
  88. $parts=getdate(strtotime($date));
  89. $days=cal_days_in_month(CAL_GREGORIAN,$parts['mon'],$parts['year']);
  90. $ref=date('w',strtotime(date('Y-m',$parts[0]).'-01'))+(7-$first)%7;
  91. $out=[];
  92. for ($i=0;$i<$days;$i++)
  93. $out[floor(($ref+$i)/7)][($ref+$i)%7]=$i+1;
  94. }
  95. return $out;
  96. }
  97. }