added array helper functions

This commit is contained in:
2023-03-12 13:51:01 +10:00
parent 3f48f11838
commit 8244230268
2 changed files with 47 additions and 1 deletions

40
app/Helpers/Array.php Normal file
View 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));
}

View File

@@ -2,7 +2,10 @@
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"require": {
"php": "^8.0.2",
@@ -26,6 +29,9 @@
"spatie/laravel-ignition": "^1.0"
},
"autoload": {
"files": [
"app/Helpers/Array.php"
],
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",