PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/functions/funcs.arrays.php

https://gitlab.com/fork/hotarucms
PHP | 204 lines | 104 code | 26 blank | 74 comment | 22 complexity | a356de31ce559724cdeb4261e8440de9 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * A collection of functions for manipulating arrays
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE: Hotaru CMS is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation, either version 3 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * Hotaru CMS is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with Hotaru CMS. If not, see http://www.gnu.org/licenses/.
  18. *
  19. * @category Content Management System
  20. * @package HotaruCMS
  21. * @author Hotaru CMS Team
  22. * @copyright Copyright (c) 2009 - 2013, Hotaru CMS
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
  24. * @link http://www.hotarucms.org/
  25. */
  26. /**
  27. * Sort an associative array by by the key of a sub-array
  28. *
  29. * @param array $array associative array
  30. * @param string $subkey key to sort from sub-array
  31. * @param string $type "int" or "char"
  32. * @param bool $sort_ascending sort order
  33. * @return array
  34. *
  35. * Note: http://us2.php.net/manual/en/function.ksort.php
  36. */
  37. function sksort(&$array, $subkey="id", $type="int", $sort_ascending=false)
  38. {
  39. if (empty($array)) { return false; }
  40. if (count($array)) {
  41. $temp_array[key($array)] = array_shift($array);
  42. }
  43. foreach ($array as $key => $val)
  44. {
  45. $offset = 0;
  46. $found = false;
  47. foreach ($temp_array as $tmp_key => $tmp_val)
  48. {
  49. if ($type == "int") {
  50. if (!$found && ($val[$subkey]) > ($tmp_val[$subkey]))
  51. {
  52. $temp_array = array_merge(
  53. (array)array_slice($temp_array,0,$offset),
  54. array($key => $val),
  55. array_slice($temp_array,$offset)
  56. );
  57. $found = true;
  58. }
  59. } else {
  60. if (!$found && strtolower($val[$subkey]) > strtolower($tmp_val[$subkey]))
  61. {
  62. $temp_array = array_merge(
  63. (array)array_slice($temp_array,0,$offset),
  64. array($key => $val),
  65. array_slice($temp_array,$offset)
  66. );
  67. $found = true;
  68. }
  69. }
  70. $offset++;
  71. }
  72. if (!$found) {
  73. $temp_array = array_merge($temp_array, array($key => $val));
  74. }
  75. }
  76. if ($sort_ascending) { $array = array_reverse($temp_array); }
  77. else $array = $temp_array;
  78. return $array;
  79. }
  80. /**
  81. * Is in case insensitive array
  82. *
  83. * @link http://jp.php.net/array_unique
  84. */
  85. function in_iarray($str, $a)
  86. {
  87. foreach($a as $v) {
  88. if (strcasecmp($str, $v) == 0) { return true;}
  89. }
  90. return false;
  91. }
  92. /**
  93. * Is unique in case insensitive array
  94. *
  95. * @link http://jp.php.net/array_unique
  96. */
  97. function array_iunique($a)
  98. {
  99. $n = array();
  100. foreach ($a as $k=>$v) {
  101. if (!in_iarray($v, $n)) { $n[$k] = $v; }
  102. }
  103. return $n;
  104. }
  105. /**
  106. * Is serialized?
  107. *
  108. * @param mixed $data
  109. * @return bool
  110. * @link http://www.weberdev.com/get_example-4099.html
  111. */
  112. function is_serialized($data)
  113. {
  114. if (!$data || !is_string($data)) {
  115. return false;
  116. }
  117. if (preg_match("/^(i|s|a|o|d)(.*);/si",$data)) {
  118. return true;
  119. }
  120. return false;
  121. }
  122. /**
  123. * Convert/Parse Object to Array
  124. *
  125. * @param $object
  126. * @return bool
  127. * @link http://forum.weblivehelp.net/web-development/php-convert-array-object-and-vice-versa-t2.html
  128. */
  129. function parse_object_to_array($object)
  130. {
  131. $array = array();
  132. if (is_object($object)) {
  133. foreach($object as $item)
  134. array_push($array, $item);
  135. }
  136. return $array;
  137. }
  138. /**
  139. * Parse array to object
  140. *
  141. * @param <type> $array
  142. * @return <type>
  143. * @link http://www.lost-in-code.com/programming/php-code/php-array-to-object/
  144. */
  145. function parse_array_to_object($array = array())
  146. {
  147. if (!empty($array)) {
  148. $data = false;
  149. foreach ($array as $akey => $aval) {
  150. $data -> {$akey} = $aval;
  151. }
  152. return $data;
  153. }
  154. return false;
  155. }
  156. /**
  157. * Remove item from array
  158. *
  159. * @param array $array
  160. * @param string $str
  161. * @return array
  162. * @link http://php.net/manual/en/ref.array.php - jan at hooda dot de
  163. */
  164. function array_remove($array, $str)
  165. {
  166. if (in_array($str,$array)==true)
  167. {
  168. foreach ($array as $key=>$value)
  169. {
  170. if ($value==$str)
  171. {
  172. unset($array[$key]);
  173. }
  174. }
  175. }
  176. return $array;
  177. }
  178. ?>