PHP accurately determines if an array is indexed
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)? ' Indexed array ': ' is not an indexed array '; Echo '
"; Echo is_assoc2 ($array)?" Indexed array ': ' is not an indexed array '; Echo '
"; $array = Array (" 0 "=" 1 "," 1 "=" 3 "); Var_dump ($array); Echo Is_assoc ($array)? ' Indexed array ': ' is not an indexed array '; Echo '
"; Echo is_assoc2 ($array)?" Indexed array ': ' is not an indexed array '; Echo '
"; $array = Array (" name "=" 1 "," Age "=" 3 "); Var_dump ($array); Echo Is_assoc ($array)? ' Indexed array ': ' is not an indexed array '; Echo '
"; Echo is_assoc2 ($array)?" Indexed array ': ' is not an indexed array ';
?
Array (size=2) 0 = string ' 1 ' (length=1) 1 = = String ' 3 ' (length=1) not an indexed array is not an indexed array (size=2) 0 = s Tring ' 1 ' (length=1) 1 = String ' 3 ' (length=1) not an indexed array is not an indexed array (size=2) ' name ' = = String ' 1 ' (length=1)
' age ' = = String ' 3 ' (length=1) index array of indexed arrays
?