We want a list of the flags which have the value of 1 to be returned. NOTE: A real use case might be that we have a list of possible flags which in a database we have stored whether each of the flags are 0 or 1. Using is_array prior to an in_array within an if clause will safely escape a check against a variable that could potentially be a non-array when using in_array. If you are not concerned about the actual order of the indices, you can change the comparison to = and != respectively. The following functions will work for PHP >= 4. Otherwise it can be assumed to be an associative array (something unobtainable in languages such as C). The result must match the array passed to it in order to truly be a numerically indexed (sequential) array. If only one array is given and the array is numerically indexed, the keys get re-indexed in a continuous way. The following solution utilizes the array_merge properties. A true sequential array would be in consecutive order with no gaps in the indices. The functions fail to recognize indexes that are not in sequence or in order. The is_associative_array() and is_sequential_array() functions posted by 'rjg4013 at rit dot edu' are not accurate. Set this now so the first for loop doesn't do the extra work. You can use this script to test the speed of both methods. On my machine, this method takes about 1/4 the time of using is_array().Ĭast the value to an array, then check (using =) if it is identical to the original. If you use is_array() millions of times, you will notice a *huge* difference. I've found a faster way of determining an array. Getting Started Introduction A simple tutorial Language Reference Basic syntax Types Variables Constants Expressions Operators Control Structures Functions Classes and Objects Namespaces Enumerations Errors Exceptions Fibers Generators Attributes References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Predefined Attributes Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security Filesystem Security Database Security Error Reporting User Submitted Data Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent Database Connections Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts ? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search
0 Comments
Leave a Reply. |