You may achieve this by using an associative list.Consider the following scenario: you have a group of people and want to assign gender to each of them based on their names.The variable’s name is “$variable name…”, the element’s access index number is “,” and the array element’s value is “value.”.Instead, you may create an associative list with the workers’ names as keys and their salary as the value. An associative array’s index is a string that establishes a strong relationship between key and value.įor storing employee wages in an array, a numerically indexed array is not the ideal choice. Returns the values in $array as a non-associative array.Associative arrays are similar to numeric arrays in appearance, but differ in terms of index. Like the standard array\_pop() PHP function, pops the last item of the specified array and returns its value. Sorts the items of $array by their key names. $key = $array -> keyname ( $index ) Įcho $array -> keyname ( 1 ) $array -> ksort ( ) The method returns true if $array contains $value, and false otherwise. $status = $array -> in\_subarray ( $value ) Ĭhecks if an array contains the specified value in one of its nested array items. $status = $array -> in\_array ( $value ) Ĭhecks if an array contains the specified value. $status = $array -> iin\_subarray ( $value ) Ĭhecks if an array contains the specified value in one of its nested array items. The following example will echo the value "B" : $array = new AssociativeArray ( ) Įcho $array -> keyname ( 1 ) $status = $array -> iin\_array ( $value ) Ĭhecks if an array contains the specified value. Returns the array key at the specified integer index. Note also that you can access items using either the array notation or the object notation the following two lines of code, using the array initialized in the above example, will display the same element, "Value of A" : echo $array Īssociative array items can be also accessed by their numerical index : echo $array // Will echo "Value of B" You can also use the isset() method to check for the existence of an individual element in an array, and unset() to remove a specific element from an array. Loop through array items using a traditional for() loop The class implements the ArrayAccess, Countable and IteratorAggregate interfaces this means that you can use the following constructs : // Create an arrayĮcho count ( $array ) // will output "4" However, if you have additional needs, you can freely add the methods of your choice or simply contact me to provide a new version I will be happy to enrich this class based on your suggestions. You may find the list of methods contained in this class a little bit poor and you will be true : I wanted to have a class that exactly fits my day-to-day needs, and nothing more (the day-to-day needs in question were very basic). Of course, it may seem incomplete (for example, it does not implement all the various sorting functions that you may expect), but it will fit your needs for your day-to-day purposes.Īnd, of course, it has been optimized to avoid unnecessary computations whenever you insert or delete an item from the array. This is the only reason why I developed this class. Since PHP arrays internally use hash tables, where the array keys are case-sensitive hashed values, it is impossible that one day you will be able to use associative arrays with case-insensitive keys. ![]() This is why I developed this class, which I'm heavily using each time I need an associative array with case-insensitive keys. I realized one day that I had duplicated a little bit elsewhere the same code that allowed certain associative arrays to be accessed with case-insensitive keys and I told myself that other developers may face the same situtation and may suffer from typos in their code, because they specified an uppercase 'A' instead of lowercase 'a' when accessing an array item by its key. INI file, such as php.ini, and always wanted key names to be case-insensisitive. The origin of this class comes from a habit of mine : I'm using several configuration files that have the format of a Windows. WHY HAVING ASSOCIATIVE ARRAY WITH KEYS THAT ARE NOT CASE-SENSITIVE ? Both lines of code will display the same result :Įcho "Getting value of array : ". ![]() Retrieve item 'A', without caring about case-sensitivity. ![]() It tries to mimic the built-in PHP array type to provide as much transparent access as possible.Ī basic example would be : // Create an array The AssociativeArray class has been designed to implement associative arrays whose keys will be case-insensitive.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |