PHP accurately determines whether it is an index array

巴扎黑
Release: 2016-11-21 13:34:57
Original
1425 people have browsed it

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)?&#39;索引数组&#39;:&#39;不是索引数组&#39;;
echo "<br/>";
$array = array("0"=>"1","1"=>"3");
var_dump($array);
echo is_assoc($array)?&#39;索引数组&#39;:&#39;不是索引数组&#39;;
echo "<br/>";
echo is_assoc2($array)?&#39;索引数组&#39;:&#39;不是索引数组&#39;;
echo "<br/>";
$array = array("name"=>"1","age"=>"3");
var_dump($array);
echo is_assoc($array)?&#39;索引数组&#39;:&#39;不是索引数组&#39;;
echo "<br/>";
echo is_assoc2($array)?&#39;索引数组&#39;:&#39;不是索引数组&#39;;
Copy after login
array (size=2)
  0 => string &#39;1&#39; (length=1)
  1 => string &#39;3&#39; (length=1)
不是索引数组
不是索引数组
array (size=2)
  0 => string &#39;1&#39; (length=1)
  1 => string &#39;3&#39; (length=1)
不是索引数组
不是索引数组
array (size=2)
  &#39;name&#39; => string &#39;1&#39; (length=1)
  &#39;age&#39; => string &#39;3&#39; (length=1)
索引数组
索引数组
Copy after login



Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template