Advertisement
bkader

CodeIgniter MY_array_helper

Jan 1st, 2016
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.44 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. /**
  5.  * Extend CodeIgniter Array Helper
  6.  *
  7.  * @package     CodeIgniter
  8.  * @subpackage  Helpers
  9.  * @category    Array Helper
  10.  * @author      Kader Bouyakoub <bkader@mail.com>
  11.  * @link        @KaderBouyakoub
  12.  */
  13.  
  14. /**
  15.  * Use of PHP : array_change_key_case
  16.  *
  17.  * @param   object
  18.  * @param   string
  19.  * @return  object
  20.  */
  21. if ( ! function_exists('object_change_key_case ')) {
  22.     function object_change_key_case($object, $case = NULL) {
  23.         is_array($object) OR $object = (array) $object;
  24.         // You can use alias: 'lower', 'lowercase', 'upper', 'uppercase'
  25.         switch ($case) {
  26.             // case of lowercased
  27.             case CASE_LOWER:
  28.             case 'lower':
  29.             case 'lowercase':
  30.                 $case = CASE_LOWER;
  31.             break;
  32.             // case of uppercased
  33.             case CASE_UPPER:
  34.             case 'upper':
  35.             case 'uppercase':
  36.                 $case = CASE_UPPER;
  37.             break;
  38.             // By default and in case the passed parameter
  39.             // is not one of the aliases or the PHP ones
  40.             default:
  41.                 $case = CASE_LOWER;
  42.             break;
  43.         }
  44.         return (object) array_change_key_case($object, $case);
  45.     }
  46. }
  47.  
  48. /**
  49.  * Use of PHP : array_chunk
  50.  *
  51.  * @param   object
  52.  * @param   string
  53.  * @return  array
  54.  */
  55. if ( ! function_exists('object_chunk')) {
  56.     function object_chunk($object, $size = 1, $preserve_keys = FALSE) {
  57.         is_array($object) OR $object = (array) $object;
  58.         is_bool($preserve_keys) OR $preserve_keys = (bool) $preserve_keys;
  59.         return array_chunk($object, $size, $preserve_keys);
  60.     }
  61. }
  62.  
  63. /**
  64.  * Use of PHP : array_column
  65.  *
  66.  * (PHP 5 >= 5.5.0, PHP 7)
  67.  *
  68.  * @param   object
  69.  * @param   string
  70.  * @return  array
  71.  */
  72. if ( ! function_exists('object_column')) {
  73.     function object_column($object, $column_key, $index_key = NULL) {
  74.         is_array($object) OR $object = (array) $object;
  75.         foreach ($object as &$child) {
  76.             $child = (array) $child;
  77.         }
  78.         return array_column($object, $column_key, $index_key);
  79.     }
  80. }
  81.  
  82. /**
  83.  * Use of PHP : array_combine
  84.  *
  85.  * (PHP 5, PHP 7)
  86.  *
  87.  * @param   object
  88.  * @param   string
  89.  * @return  object
  90.  */
  91. if ( ! function_exists('object_combine')) {
  92.     function object_combine($keys, $values) {
  93.         is_array($keys) OR $keys = (array) $keys;
  94.         is_array($values) OR $values = (array) $values;
  95.         return (object) array_combine($keys, $values);
  96.     }
  97. }
  98.  
  99. /**
  100.  * Use of PHP : array_merge
  101.  *
  102.  * @param   object
  103.  * @param   string
  104.  * @return  object
  105.  */
  106. if ( ! function_exists('object_merge')) {
  107.     function object_merge($obj1, $obj2) {
  108.         if ( ! $obj1 OR ! $obj2) return FALSE;
  109.         return (object) array_merge((array) $obj1, (array) $obj2);
  110.     }
  111. }
  112.  
  113. /**
  114.  * Use of PHP : array_key_exists
  115.  *
  116.  * @param   string
  117.  * @param   object
  118.  * @return  boolean
  119.  */
  120. if ( ! function_exists('object_key_exists')) {
  121.     function object_key_exists($key, $search) {
  122.         is_array($search) OR $search = (array) $search;
  123.         return array_key_exists($key, $search);
  124.     }
  125. }
  126.  
  127. /**
  128.  * Use of PHP : array_keys
  129.  *
  130.  * @param   object
  131.  * @param   string
  132.  * @param   boolean
  133.  * @return  array
  134.  */
  135. if ( ! function_exists('object_keys')) {
  136.     function object_keys($object, $search_value = NULL, $strict = FALSE) {
  137.         is_array($object) OR $object = (array) $object;
  138.         return array_keys($object, $search_value, $strict);
  139.     }
  140. }
  141.  
  142. /**
  143.  * Use of PHP : array_map
  144.  *
  145.  * @param   object
  146.  * @param   mixed
  147.  * @return  array
  148.  */
  149. if ( ! function_exists('object_map')) {
  150.     function object_map($object, $callback) {
  151.         is_array($object) OR $object = (array) $object;
  152.         return array_map($callback, $object);
  153.     }
  154. }
  155.  
  156. /**
  157.  * Use of PHP : array_pop
  158.  *
  159.  * @param   object
  160.  * @return  array
  161.  */
  162. if ( ! function_exists('object_pop')) {
  163.     function object_pop($object) {
  164.         is_array($object) OR $object = (array) $object;
  165.         return array_pop($object);
  166.     }
  167. }
  168.  
  169. /**
  170.  * Use of PHP : array_rand
  171.  *
  172.  * @param   object
  173.  * @param   integer
  174.  * @return  array
  175.  */
  176. if ( ! function_exists('object_rand')) {
  177.     function object_rand($object, $num = 1) {
  178.         is_array($object) OR $object = (array) $object;
  179.         return array_rand($object, $num);
  180.     }
  181. }
  182.  
  183. /**
  184.  * Use of PHP : array_search
  185.  *
  186.  * @param   object
  187.  * @param   integer
  188.  * @return  array
  189.  */
  190. if ( ! function_exists('object_search')) {
  191.     function object_search($needle, $haystack, $strict = FALSE) {
  192.         is_array($haystack) OR $haystack = (array) $haystack;
  193.         return array_search($needle, $haystack, $strict);
  194.     }
  195. }
  196.  
  197. /**
  198.  * Use of PHP : array_shift
  199.  *
  200.  * @param   object
  201.  * @return  array
  202.  */
  203. if ( ! function_exists('object_shift')) {
  204.     function object_shift($object) {
  205.         is_array($object) OR $object = (array) $object;
  206.         return array_shift($object);
  207.     }
  208. }
  209.  
  210. /**
  211.  * Use of PHP : array_slice
  212.  *
  213.  * @param   object
  214.  * @return  array
  215.  */
  216. if ( ! function_exists('object_slice')) {
  217.     function object_slice($object, $offset = 0, $length = NULL, $preserve_keys = FALSE) {
  218.         is_array($object) OR $object = (array) $object;
  219.         return array_slice($object, $offset, $length, $preserve_keys);
  220.     }
  221. }
  222.  
  223. /* End of file MY_array_helper.php */
  224. /* Location: ./application/helpers/MY_array_helper.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement