is_array() - php 变量处理函数
is_array()
(PHP 4, PHP 5, PHP 7)
检测变量是否是数组
描述
is_array(mixed $var): bool如果$var是array,则返回TRUE
,否则返回FALSE
。
参见is_float()、is_int()、is_integer()、is_string()和is_object()。
Please note that the 'cast to array' check is horrendously out of date. Running that code against PHP 5.6 results in this: is_array : 0.93975400924683 cast, === : 1.2425191402435 So, please use 'is_array', not the horrible casting hack.
Or you could make use of the array_diff_key and array_key function: In my tests it runs about twice as fast as Michael/Gabriel's array_reduce() method. (Speaking of which: Gabriel's version doesn't work as written; it reports associative arrays as numeric if only the first key is non-numeric, or if the keys are numeric but ordered backwards. Michael solves this problem by comparing array_reduce() to count(), but that costs another function call; it also works to just compare to -1 instead of 0, and therefore return -1 as the ternary else from the callback).
I've found a faster way of determining an array. If you use is_array() millions of times, you will notice a *huge* difference. On my machine, this method takes about 1/4 the time of using is_array(). Cast the value to an array, then check (using ===) if it is identical to the original. You can use this script to test the speed of both methods.What's faster for determining arrays?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. For instance: 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. We want a list of the flags which have the value of 1 to be returned. Our example here will not use so many technical artifacts, but will be based on similar logic just to get the point across. Hope that can help someone, I know it helped me.alex frase's example is fast but elanthis at awesomeplay dot com's example is faster and Ilgar's modification of alex's code is faulty (the part " || $_array[$k] !== $v"). Also, Ilgar's suggestion of giving a false return value when the variable isnt an array is not suitable in my opinion and i think checking if the array is empty would also be a suitable check before the rest of the code runs. So here's the modified (is_vector) version and the modified (alex's is_assoc) versionI would change the order of the comparison, because if it is really an empty array, it is better to stop at that point before doing several 'cpu & memory intensive' function calls. In the end on a ratio of 3 not empty arrays to 1 empty array computed for 1000000 iterations it needed 10% less time. Or the other way round: It needed approx 3% to 4% more time if the array is not empty, but was at least 4 times faster on empty arrays. Additionally the memory consumption veritably lesser.The is_associative_array() and is_sequential_array() functions posted by 'rjg4013 at rit dot edu' are not accurate. The functions fail to recognize indexes that are not in sequence or in order. For example, array(0=>'a', 2=>'b', 1=>'c') and array(0=>'a', 3=>'b', 5=>'c') would be considered as sequential arrays. A true sequential array would be in consecutive order with no gaps in the indices. The following solution utilizes the array_merge properties. If only one array is given and the array is numerically indexed, the keys get re-indexed in a continuous way. The result must match the array passed to it in order to truly be a numerically indexed (sequential) array. Otherwise it can be assumed to be an associative array (something unobtainable in languages such as C). The following functions will work for PHP >= 4. If you are not concerned about the actual order of the indices, you can change the comparison to == and != respectively.An error will be thrown If is_array() is applied to a nonexisting varble.function is_associate_array($array) { return $array === array_values($array); } or you can add check is_array in functionsAnd here is another variation for a function to test if an array is associative. Based on the idea by mot4h.A slight modification of what's below:Notice that the benchmark results from hperrin at gmail dot com have changed in the meantime: is_array : 0.31888604164124 cast, === : 0.58448791503906 (Using PHP 5.6.24, I expect other results with PHP 7)Using empty() in the previous example posted by Anonymous will result in a "Fatal error: Can't use function return value in write context". I suggest using count() instead:I'm using PHP 5.3.10 and the remark from hperrin is not (no longer?) valid is_array is (now?) faster than if ( (array) $unknown !== $unknown ) my result of hperrins test script is_array : 0.61710119247437 cast, === : 1.1551909446716The next post is not correct because has problems with blank array index: https://www.php.net/manual/es/function.is-array.php#89332 The next code use the above link php code is_assoc1() - uses unset(), a bit slow, but mem friendly and no function calls is_assoc2() - uses isset(), fastest one, but returns TRUE whenever array contains NULL is_assoc3() - fixed is_assoc2(), uses array_key_exists(), fast and memory friendly, and much smarter than the following (no need to check all those keys) is_assoc4() - alex' version with proper check and key sorting is_assoc5() - fixed a bit JTS' version, really nice one, but uses too many functions and checks all keys Results: is_assoc1(): 2.1628699302673 is_assoc2(): 1.1079933643341 is_assoc3(): 1.7120850086212 is_assoc4(): 3.9194552898407 is_assoc5(): 1.9509885311127This is probably the best way to test for associative arrays: This version is based on the one from dmitry dot sushilov, which is unfortunately exactly opposite and misses the is_array and empty test. http://php.net/manual/en/function.is-array.php#115901Will check a Multi-Dimentional Array to any specified level. This is a fix to 11/16/05 submission, which would break since you must supply a foreach with an array. Beware recursive functions shouldn't go over 100 deep or could break the memory stack on server.Mike's function is quite cool, it is just the one, I was searching for. Using range is a great idea! But it's a bit long for me. Here is a shorter version: Or, if you don't want to type that much:Or you could make use of the array_diff_key and range functions:Yet another associative array test:hperrin at gmail dot com i tested modified version of your code: Results are quite different! PHP 5.5: is_array - execution time is constant over different $size cast, === - execution time is proportional with $size, for small arrays, it's faster than is_array, but for bigger arrays, it's way slower. Also, bigger memory allocation because casting actually allocates memory PHP 7.0: is_array and cast, === have constant execution time over different $size: is_array : 0.12564396858215 cast, === : 0.24629783630371 Looks like casting is optimised, but still slower. Bottom line is: optimisation is not done for special scenario, but for average use and scalability. Don't reinvent wheel.Simple check for a Multi-Dimentional Array of any depthIf you wanted to have 'pure' associative arrays, you could filter out non-string keys and then compare the count of the original array with the count of the filtered array. I've tested the point of checking whether the array is empty first. The overall result was a 200% speed increase when the array was indeed empty, but an average 10% slow when it had elements inside.Yet another safer, faster way of detecting whether an array is associative. The principle is: using array reduction on the keys, we verify that each key is numeric and is equal to its rank. Beware: integer keys that are not in sequence, or are negative, or with "holes", still make an associative array. Of course, it is still faster if the callback for array_reduce is not an anonymous function:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)