ing $keys * @return array */ public static function only($array, $keys) { return array_intersect_key($array, array_flip((array) $keys)); } /** * Pluck an array of values from an array. * * @param mixed[] $array * @param string|mixed[] $value * @param string|mixed[]|null $key * @return mixed[] */ public static function pluck($array, $value, $key = null) { $results = []; list($value, $key) = static::explodePluckParameters($value, $key); foreach ($array as $item) { $itemValue = data_get($item, $value); // If the key is "null", we will just append the value to the array and keep // looping. Otherwise we will key the array using the value of the key we // received from the developer. Then we'll return the final array form. if (is_null($key)) { $results[] = $itemValue; } else { $itemKey = data_get($item, $key); $results[$itemKey] = $itemValue; } } return $results; } /** * Explode the "value" and "key" arguments passed to "pluck". * * @param string|mixed[] $value * @param string|mixed[]|null $key * @return mixed[] */ protected static function explodePluckParameters($value, $key) { $value = is_string($value) ? explode('.', $value) : $value; $key = is_null($key) || is_array($key) ? $key : explode('.', $key); return [$value, $key]; } /** * Push an item onto the beginning of an array. * * @param mixed[] $array * @param mixed $value * @param mixed $key * @return mixed[] */ public static function prepend($array, $value, $key = null) { if (is_null($key)) { array_unshift($array, $value); } else { $array = [$key => $value] + $array; } return $array; } /** * Get a value from the array, and remove it. * * @param mixed[] $array * @param string $key * @param mixed $default * @return mixed */ public static function pull(&$array, $key, $default = null) { $value = static::get($array, $key, $default); static::forget($array, $key); return $value; } /** * Set an array item to a given value using "dot" notation. * * If no key is given to the method, the entire array will be replaced. * * @param mixed[] $array * @param string $key * @param mixed $value * @return mixed[] */ public static function set(&$array, $key, $value) { if (is_null($key)) { return $array = $value; } $keys = explode('.', $key); while (count($keys) > 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; } /** * Sort the array using the given callback or "dot" notation. * * @param mixed[] $array * @param callable|string $callback * @return mixed[] */ public static function sort($array, $callback) { return Collection::make($array)->sortBy($callback)->all(); } /** * Recursively sort an array by keys and values. * * @param mixed[] $array * @return mixed[] */ public static function sortRecursive($array) { foreach ($array as &$value) { if (is_array($value)) { $value = static::sortRecursive($value); } } if (static::isAssoc($array)) { ksort($array); } else { sort($array); } return $array; } /** * Filter the array using the given callback. * * @param mixed[] $array * @param callable $callback * @return mixed[] */ public static function where($array, callable $callback) { return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH); } /** * @param mixed ...$arrays * * @return mixed[]|mixed[mixed] */ public static function crossJoin(...$arrays) { $results = [[]]; foreach ($arrays as $index => $array) { $append = []; foreach ($results as $product) { foreach ($array as $item) { $product[$index] = $item; $append[] = $product; } } $results = $append; } return $results; } }