matrix.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /*
  3. Copyright (c) 2009-2019 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. * select a subset of fields from an input array
  34. * @param string|array $fields splittable string or array
  35. * @param string|array $data hive key or array
  36. * @return array
  37. */
  38. function select($fields, $data) {
  39. return array_intersect_key(is_array($data) ? $data : \Base::instance()->get($data),
  40. array_flip(is_array($fields) ? $fields : \Base::instance()->split($fields)));
  41. }
  42. /**
  43. * walk with a callback function through a subset of fields from an input array
  44. * the callback receives the value, index-key and the full input array as parameters
  45. * set value parameter as reference and you're able to modify the data as well
  46. * @param string|array $fields splittable string or array of fields
  47. * @param string|array $data hive key or input array
  48. * @param callable $callback (mixed &$value, string $key, array $data)
  49. * @return array modified subset data
  50. */
  51. function walk($fields, $data, $callback) {
  52. $subset=$this->select($fields, $data);
  53. array_walk($subset, $callback, $data);
  54. return $subset;
  55. }
  56. /**
  57. * Rotate a two-dimensional array variable
  58. * @return NULL
  59. * @param $var array
  60. **/
  61. function transpose(array &$var) {
  62. $out=[];
  63. foreach ($var as $keyx=>$cols)
  64. foreach ($cols as $keyy=>$valy)
  65. $out[$keyy][$keyx]=$valy;
  66. $var=$out;
  67. }
  68. /**
  69. * Sort a multi-dimensional array variable on a specified column
  70. * @return bool
  71. * @param $var array
  72. * @param $col mixed
  73. * @param $order int
  74. **/
  75. function sort(array &$var,$col,$order=SORT_ASC) {
  76. uasort(
  77. $var,
  78. function($val1,$val2) use($col,$order) {
  79. list($v1,$v2)=[$val1[$col],$val2[$col]];
  80. $out=is_numeric($v1) && is_numeric($v2)?
  81. Base::instance()->sign($v1-$v2):strcmp($v1,$v2);
  82. if ($order==SORT_DESC)
  83. $out=-$out;
  84. return $out;
  85. }
  86. );
  87. $var=array_values($var);
  88. }
  89. /**
  90. * Change the key of a two-dimensional array element
  91. * @return NULL
  92. * @param $var array
  93. * @param $old string
  94. * @param $new string
  95. **/
  96. function changekey(array &$var,$old,$new) {
  97. $keys=array_keys($var);
  98. $vals=array_values($var);
  99. $keys[array_search($old,$keys)]=$new;
  100. $var=array_combine($keys,$vals);
  101. }
  102. /**
  103. * Return month calendar of specified date, with optional setting for
  104. * first day of week (0 for Sunday)
  105. * @return array
  106. * @param $date string|int
  107. * @param $first int
  108. **/
  109. function calendar($date='now',$first=0) {
  110. $out=FALSE;
  111. if (extension_loaded('calendar')) {
  112. if (is_string($date))
  113. $date=strtotime($date);
  114. $parts=getdate($date);
  115. $days=cal_days_in_month(CAL_GREGORIAN,$parts['mon'],$parts['year']);
  116. $ref=date('w',strtotime(date('Y-m',$parts[0]).'-01'))+(7-$first)%7;
  117. $out=[];
  118. for ($i=0;$i<$days;$i++)
  119. $out[floor(($ref+$i)/7)][($ref+$i)%7]=$i+1;
  120. }
  121. return $out;
  122. }
  123. }