Home >Backend Development >PHP Tutorial >php精确判断是否为目录数组

php精确判断是否为目录数组

WBOY
WBOYOriginal
2016-06-13 11:56:31838browse

php精确判断是否为索引数组

function is_assoc($arr){    return array_keys($arr) !== range(0, count($arr) - 1);	// array_values($arr) !== $arr}function is_assoc2($array) {  return (bool)count(array_filter(array_keys($array), 'is_string'));}$array = array(0=>"1",1=>"3");var_dump($array);echo is_assoc($array)?'索引数组':'不是索引数组';echo "<br>";echo is_assoc2($array)?'索引数组':'不是索引数组';echo "<br>";$array = array("0"=>"1","1"=>"3");var_dump($array);echo is_assoc($array)?'索引数组':'不是索引数组';echo "<br>";echo is_assoc2($array)?'索引数组':'不是索引数组';echo "<br>";$array = array("name"=>"1","age"=>"3");var_dump($array);echo is_assoc($array)?'索引数组':'不是索引数组';echo "<br>";echo is_assoc2($array)?'索引数组':'不是索引数组';

?

array (size=2)  0 => string '1' (length=1)  1 => string '3' (length=1)不是索引数组不是索引数组array (size=2)  0 => string '1' (length=1)  1 => string '3' (length=1)不是索引数组不是索引数组array (size=2)  'name' => string '1' (length=1)  'age' => string '3' (length=1)索引数组索引数组

?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn