summaryrefslogtreecommitdiff
path: root/system/helpers/arr.php
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2009-11-24 19:20:36 -0800
committerBharat Mediratta <bharat@menalto.com>2009-11-24 19:20:36 -0800
commit9b6663f87a7e679ffba691cf516191fc840cf978 (patch)
tree20cf9f3aaf93b4ba69d282dcf10d259db4a752de /system/helpers/arr.php
parent82ee5f9d338017c69331b2907f37a468ced8c66e (diff)
Update to Kohana r4684 which is now Kohana 2.4 and has substantial
changes.
Diffstat (limited to 'system/helpers/arr.php')
-rw-r--r--system/helpers/arr.php103
1 files changed, 33 insertions, 70 deletions
diff --git a/system/helpers/arr.php b/system/helpers/arr.php
index 9570c4b5..a1bde230 100644
--- a/system/helpers/arr.php
+++ b/system/helpers/arr.php
@@ -2,12 +2,12 @@
/**
* Array helper class.
*
- * $Id: arr.php 4346 2009-05-11 17:08:15Z zombor $
+ * $Id: arr.php 4680 2009-11-10 01:57:00Z isaiah $
*
* @package Core
* @author Kohana Team
- * @copyright (c) 2007-2008 Kohana Team
- * @license http://kohanaphp.com/license.html
+ * @copyright (c) 2007-2009 Kohana Team
+ * @license http://kohanaphp.com/license
*/
class arr_Core {
@@ -102,20 +102,26 @@ class arr_Core {
$found = array();
foreach ($keys as $key)
{
- if (isset($search[$key]))
- {
- $found[$key] = $search[$key];
- }
- else
- {
- $found[$key] = NULL;
- }
+ $found[$key] = isset($search[$key]) ? $search[$key] : NULL;
}
return $found;
}
/**
+ * Get the value of array[key]. If it doesn't exist, return default.
+ *
+ * @param array array to search
+ * @param string key name
+ * @param mixed default value
+ * @return mixed
+ */
+ public static function get(array $array, $key, $default = NULL)
+ {
+ return isset($array[$key]) ? $array[$key] : $default;
+ }
+
+ /**
* Because PHP does not have this function.
*
* @param array array to unshift
@@ -152,44 +158,6 @@ class arr_Core {
}
/**
- * @param mixed $needle the value to search for
- * @param array $haystack an array of values to search in
- * @param boolean $sort sort the array now
- * @return integer|FALSE the index of the match or FALSE when not found
- */
- public static function binary_search($needle, $haystack, $sort = FALSE)
- {
- if ($sort)
- {
- sort($haystack);
- }
-
- $high = count($haystack) - 1;
- $low = 0;
-
- while ($low <= $high)
- {
- $mid = ($low + $high) >> 1;
-
- if ($haystack[$mid] < $needle)
- {
- $low = $mid + 1;
- }
- elseif ($haystack[$mid] > $needle)
- {
- $high = $mid - 1;
- }
- else
- {
- return $mid;
- }
- }
-
- return FALSE;
- }
-
-
- /**
* Emulates array_merge_recursive, but appends numeric keys and replaces
* associative keys, instead of appending all keys.
*
@@ -264,27 +232,6 @@ class arr_Core {
}
/**
- * Fill an array with a range of numbers.
- *
- * @param integer stepping
- * @param integer ending number
- * @return array
- */
- public static function range($step = 10, $max = 100)
- {
- if ($step < 1)
- return array();
-
- $array = array();
- for ($i = $step; $i <= $max; $i += $step)
- {
- $array[$i] = $i;
- }
-
- return $array;
- }
-
- /**
* Recursively convert an array to an object.
*
* @param array array to convert
@@ -309,4 +256,20 @@ class arr_Core {
return $object;
}
+ /**
+ * Returns specific key/column from an array of objects.
+ *
+ * @param string|integer $key The key or column number to pluck from each object.
+ * @param array $array The array of objects to pluck from.
+ * @return array
+ */
+ public static function pluck($key, $array)
+ {
+ $result = array();
+ foreach ($array as $i => $object)
+ {
+ $result[$i] = isset($object[$key]) ? $object[$key] : NULL;
+ }
+ return $result;
+ }
} // End arr