Add Arr::nth() and Arr::duplicates()

This commit is contained in:
Giuseppe Criscione 2022-11-13 13:21:10 +01:00
parent e0436303fa
commit 42e47decc5

View File

@ -96,6 +96,15 @@ class Arr
}
}
/**
* Get the array value at the given index,
* negative indices are not allowed, use `Arr:at()` instead
*/
public static function nth(array $array, int $index)
{
return array_values($array)[$index];
}
/**
* Get the array value at the given index,
* negative indices are allowed and start from the end
@ -123,6 +132,14 @@ class Arr
return $key !== false ? $key : null;
}
/**
* Return the duplicate elements of the array
*/
public static function duplicates(array $array): array
{
return array_diff_key($array, array_unique($array));
}
/**
* Recursively append elements from the second array that are missing in the first
*/