added array helper functions
This commit is contained in:
40
app/Helpers/Array.php
Normal file
40
app/Helpers/Array.php
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/* Array Helper Functions */
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove an item from an array.
|
||||||
|
*
|
||||||
|
* @param array $arr The array to check.
|
||||||
|
* @param string|array $item The item or items to remove.
|
||||||
|
* @return array The filtered array.
|
||||||
|
*/
|
||||||
|
function arrayRemoveItem(array $arr, string|array $item): array
|
||||||
|
{
|
||||||
|
$filteredArr = $arr;
|
||||||
|
|
||||||
|
if (is_string($item) === true) {
|
||||||
|
$item = [$item];
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($item as $str) {
|
||||||
|
$filteredArr = array_filter($arr, function ($item) use ($str) {
|
||||||
|
return $item !== $str;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return $filteredArr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an array with specified the keys
|
||||||
|
*
|
||||||
|
* @param array $arr The array to filter.
|
||||||
|
* @param string|array $keys The keys to keep.
|
||||||
|
* @return array The filtered array.
|
||||||
|
*/
|
||||||
|
function arrayOnlyItems(array $arr, array $keys): array
|
||||||
|
{
|
||||||
|
return array_intersect_key($arr, array_flip($keys));
|
||||||
|
}
|
||||||
@@ -2,7 +2,10 @@
|
|||||||
"name": "laravel/laravel",
|
"name": "laravel/laravel",
|
||||||
"type": "project",
|
"type": "project",
|
||||||
"description": "The Laravel Framework.",
|
"description": "The Laravel Framework.",
|
||||||
"keywords": ["framework", "laravel"],
|
"keywords": [
|
||||||
|
"framework",
|
||||||
|
"laravel"
|
||||||
|
],
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"require": {
|
"require": {
|
||||||
"php": "^8.0.2",
|
"php": "^8.0.2",
|
||||||
@@ -26,6 +29,9 @@
|
|||||||
"spatie/laravel-ignition": "^1.0"
|
"spatie/laravel-ignition": "^1.0"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
|
"files": [
|
||||||
|
"app/Helpers/Array.php"
|
||||||
|
],
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"App\\": "app/",
|
"App\\": "app/",
|
||||||
"Database\\Factories\\": "database/factories/",
|
"Database\\Factories\\": "database/factories/",
|
||||||
|
|||||||
Reference in New Issue
Block a user