1) { $key = array_shift($keys); // If the key doesn't exist at this depth, we will just create an empty array // to hold the next value, allowing us to create the arrays to hold final // values at the correct depth. Then we'll keep digging into the array. if (!isset($array[$key]) || !is_array($array[$key])) { $array[$key] = []; } $array = &$array[$key]; } $array[array_shift($keys)] = $value; return $array; } /** * Get a subset of the items from the given array. * * @param array $array * @param array|string $keys * @return array */ public static function only($array, $keys) { $keys = (array)$keys; $results = []; foreach ($keys as $key) { static::set($results, $key, static::get($array, $key)); } return $results; } /** * Get all of the given array except for a specified array of items. * * @param array $array * @param array|string $keys * @return array */ public static function except($array, $keys) { $keys = (array)$keys; static::forget($array, $keys); return $array; } /** * Remove one or many array items from a given array using "dot" notation. * * @param array $array * @param array|string $keys * @return void */ public static function forget(&$array, $keys) { $original = &$array; $keys = (array)$keys; if (count($keys) === 0) { return; } foreach ($keys as $key) { // if the exact key exists in the top-level, remove it if (static::exists($array, $key)) { unset($array[$key]); continue; } $parts = explode('.', $key); // clean up before each pass $array = &$original; while (count($parts) > 1) { $part = array_shift($parts); if (isset($array[$part]) && is_array($array[$part])) { $array = &$array[$part]; } else { continue 2; } } unset($array[array_shift($parts)]); } } /** * Return the first element in an array passing a given truth test. * * @param array $array * @param \Closure $callback * @param mixed $default * @return mixed */ public static function first($array, $callback, $default = null) { foreach ($array as $key => $value) { if (call_user_func($callback, $key, $value)) return $value; } return value($default); } /** * Determine whether the given value is array accessible. * * @param mixed $value * @return bool */ public static function accessible($value) { return is_array($value) || $value instanceof ArrayAccess; } /** * Determine if the given key exists in the provided array. * * @param \ArrayAccess|array $array * @param string|int $key * @return bool */ public static function exists($array, $key) { if ($array instanceof ArrayAccess) { return $array->offsetExists($key); } return array_key_exists($key, $array); } /** * Return the default value of the given value. * * @param mixed $value * @return mixed */ public static function value($value) { return $value instanceof Closure ? $value() : $value; } /** * Flatten a multi-dimensional associative array with dots. * * @param array $array * @param string $prepend * * @return array */ public static function dot($array, $prepend = '') { $results = []; foreach ($array as $key => $value) { if (is_array($value) && !empty($value)) { $results = array_merge($results, static::dot($value, $prepend . $key . '.')); } else { $results[$prepend . $key] = $value; } } return $results; } public static function isTrue($array, $key) { $value = self::get($array, $key); $isFalse = !$value || $value =='false' || $value =='0'; return !$isFalse; } }