/fuel/application/helpers/MY_array_helper.php

https://github.com/rodrigowebe/FUEL-CMS · PHP · 173 lines · 87 code · 16 blank · 70 comment · 18 complexity · 93c8340bba25fd761a8bb48d1ac70dfd MD5 · raw file

  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * FUEL CMS
  4. * http://www.getfuelcms.com
  5. *
  6. * An open source Content Management System based on the
  7. * Codeigniter framework (http://codeigniter.com)
  8. *
  9. * @package FUEL CMS
  10. * @author David McReynolds @ Daylight Studio
  11. * @copyright Copyright (c) 2011, Run for Daylight LLC.
  12. * @license http://www.getfuelcms.com/user_guide/general/license
  13. * @link http://www.getfuelcms.com
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * Extends CI's array helper functions
  19. *
  20. * @package FUEL CMS
  21. * @subpackage Helpers
  22. * @category Helpers
  23. * @author David McReynolds @ Daylight Studio
  24. * @link http://www.getfuelcms.com/user_guide/helpers/my_array_helper
  25. */
  26. // --------------------------------------------------------------------
  27. /**
  28. * Array sorter that will sort on an array's key and allows for asc/desc order
  29. *
  30. * @access public
  31. * @param array
  32. * @param string
  33. * @param string
  34. * @param boolean
  35. * @param boolean
  36. * @return array
  37. */
  38. function array_sorter($array, $index, $order = 'asc', $nat_sort = FALSE, $case_sensitive = FALSE)
  39. {
  40. if(is_array($array) && count($array) > 0)
  41. {
  42. foreach (array_keys($array) as $key)
  43. {
  44. $temp[$key]=$array[$key][$index];
  45. if (! $nat_sort)
  46. {
  47. ($order == 'asc') ? asort($temp) : arsort($temp);
  48. }
  49. else
  50. {
  51. ($case_sensitive) ? natsort($temp) : natcasesort($temp);
  52. }
  53. if ($order != 'asc') $temp = array_reverse($temp,TRUE);
  54. }
  55. foreach(array_keys($temp) as $key)
  56. {
  57. (is_numeric($key)) ? $sorted[] = $array[$key] : $sorted[$key] = $array[$key];
  58. }
  59. return $sorted;
  60. }
  61. return $array;
  62. }
  63. // --------------------------------------------------------------------
  64. /**
  65. * Array sorter that will sort an array of objects based on an objects
  66. * property and allows for asc/desc order. Changes the original object
  67. *
  68. * @access public
  69. * @param mixed
  70. * @param string
  71. * @param string
  72. * @return NULL
  73. */
  74. function object_sorter(&$data, $key, $order = 'asc')
  75. {
  76. for ($i = count($data) - 1; $i >= 0; $i--)
  77. {
  78. $swapped = false;
  79. for ($j = 0; $j < $i; $j++)
  80. {
  81. if ($order == 'desc')
  82. {
  83. if ($data[$j]->$key < $data[$j + 1]->$key)
  84. {
  85. $tmp = $data[$j];
  86. $data[$j] = $data[$j + 1];
  87. $data[$j + 1] = $tmp;
  88. $swapped = true;
  89. }
  90. }
  91. else
  92. {
  93. if ($data[$j]->$key > $data[$j + 1]->$key)
  94. {
  95. $tmp = $data[$j];
  96. $data[$j] = $data[$j + 1];
  97. $data[$j + 1] = $tmp;
  98. $swapped = true;
  99. }
  100. }
  101. }
  102. if (!$swapped) return;
  103. }
  104. }
  105. // --------------------------------------------------------------------
  106. /**
  107. * Creates a key/value array based on an original array.
  108. *
  109. * Can be used in conjunction with the Form library class
  110. * (e.g. $this->form->select('countries, option_list($options)))
  111. *
  112. * @access public
  113. * @param array
  114. * @param string
  115. * @param string
  116. * @return array
  117. */
  118. function options_list($values, $value = 'id', $label = 'name')
  119. {
  120. $return = array();
  121. foreach($values as $key => $val)
  122. {
  123. if (is_array($val))
  124. {
  125. if (is_object($val)) $val = get_object_vars($val);
  126. if (!empty($val[$label])) $return[$val[$value]] = $val[$label];
  127. }
  128. else
  129. {
  130. $return[$key] = $val;
  131. }
  132. }
  133. return $return;
  134. }
  135. // --------------------------------------------------------------------
  136. /**
  137. * Parses a string in the format of key1="val1" key2="val2" into an array
  138. *
  139. * @access public
  140. * @param string
  141. * @return array
  142. */
  143. function parse_string_to_array($str)
  144. {
  145. preg_match_all('#(\w+)=([\'"])(.*)\\2#U', $str, $matches);
  146. $params = array();
  147. foreach($matches[1] as $key => $val)
  148. {
  149. if (!empty($matches[3]))
  150. {
  151. $params[$val] = $matches[3][$key];
  152. }
  153. }
  154. return $params;
  155. }
  156. /* End of file MY_array_helper.php */
  157. /* Location: ./application/helpers/MY_array_helper.php */