Depending upon use case, I would normally use something simple like the following:
abstract class DaysOfWeek
{
const Sunday = 0;
const Monday = 1;
// etc.
}
$today = DaysOfWeek::Sunday;
However, other use cases may require more validation of constants and values. Based on the comments below about reflection, and a few other notes, here's an expanded example which may better serve a much wider range of cases:
abstract class BasicEnum {
private static $constCacheArray = NULL;
private static function getConstants() {
if (self::$constCacheArray == NULL) {
self::$constCacheArray = [];
}
$calledClass = get_called_class();
if (!array_key_exists($calledClass, self::$constCacheArray)) {
$reflect = new ReflectionClass($calledClass);
self::$constCacheArray[$calledClass] = $reflect->getConstants();
}
return self::$constCacheArray[$calledClass];
}
public static function isValidName($name, $strict = false) {
$constants = self::getConstants();
if ($strict) {
return array_key_exists($name, $constants);
}
$keys = array_map('strtolower', array_keys($constants));
return in_array(strtolower($name), $keys);
}
public static function isValidValue($value, $strict = true) {
$values = array_values(self::getConstants());
return in_array($value, $values, $strict);
}
}
By creating a simple enum class that extends BasicEnum, you now have the ability to use methods thusly for simple input validation:
abstract class DaysOfWeek extends BasicEnum {
const Sunday = 0;
const Monday = 1;
const Tuesday = 2;
const Wednesday = 3;
const Thursday = 4;
const Friday = 5;
const Saturday = 6;
}
DaysOfWeek::isValidName('Humpday'); // false
DaysOfWeek::isValidName('Monday'); // true
DaysOfWeek::isValidName('monday'); // true
DaysOfWeek::isValidName('monday', $strict = true); // false
DaysOfWeek::isValidName(0); // false
DaysOfWeek::isValidValue(0); // true
DaysOfWeek::isValidValue(5); // true
DaysOfWeek::isValidValue(7); // false
DaysOfWeek::isValidValue('Friday'); // false
As a side note, any time I use reflection at least once on a static/const class where the data won't change (such as in an enum), I cache the results of those reflection calls, since using fresh reflection objects each time will eventually have a noticeable performance impact (Stored in an assocciative array for multiple enums).
Now that most people have finally upgraded to at least 5.3, and SplEnum
is available, that is certainly a viable option as well--as long as you don't mind the traditionally unintuitive notion of having actual enum instantiations throughout your codebase. In the above example, BasicEnum
and DaysOfWeek
cannot be instantiated at all, nor should they be.
Best Solution
There are different ways to delete an array element, where some are more useful for some specific tasks than others.
Deleting a single array element
If you want to delete just one array element you can use
unset()
or alternatively\array_splice()
.If you know the value and don’t know the key to delete the element you can use
\array_search()
to get the key. This only works if the element does not occur more than once, since\array_search
returns the first hit only.unset()
Note that when you use
unset()
the array keys won’t change. If you want to reindex the keys you can use\array_values()
afterunset()
, which will convert all keys to numerically enumerated keys starting from 0.Code:
Output:
\array_splice()
methodIf you use
\array_splice()
the keys will automatically be reindexed, but the associative keys won’t change — as opposed to\array_values()
, which will convert all keys to numerical keys.\array_splice()
needs the offset, not the key, as the second parameter.Code:
Output:
array_splice()
, same asunset()
, take the array by reference. You don’t assign the return values of those functions back to the array.Deleting multiple array elements
If you want to delete multiple array elements and don’t want to call
unset()
or\array_splice()
multiple times you can use the functions\array_diff()
or\array_diff_key()
depending on whether you know the values or the keys of the elements which you want to delete.\array_diff()
methodIf you know the values of the array elements which you want to delete, then you can use
\array_diff()
. As before withunset()
it won’t change the keys of the array.Code:
Output:
\array_diff_key()
methodIf you know the keys of the elements which you want to delete, then you want to use
\array_diff_key()
. You have to make sure you pass the keys as keys in the second parameter and not as values. Keys won’t reindex.Code:
Output:
If you want to use
unset()
or\array_splice()
to delete multiple elements with the same value you can use\array_keys()
to get all the keys for a specific value and then delete all elements.