dth42345 2013-03-04 20:36
浏览 55

检查数组是否不是索引数组[重复]

This question already has an answer here:

I need check if a specific Array is keyed or indexed. For instance:

// Key defined array
array('data-test' => true, 'data-object' => false);

// Indexed array
array('hello', 'world');

I can easily do a foreach with the array keys to check if all is integer. But exists a correct way to check it? A built-in PHP function?

POSSIBLE SOLUTION

// function is_array_index($array_test);
//    $array_test = array('data-test' => true, 'data-object' => false);

foreach(array_keys($array_test) as $array_key) {
    if(!is_numeric($array_key)) {
        return false;
    }
}

return true;
</div>
  • 写回答

4条回答 默认 最新

  • drj26159 2013-03-04 20:39
    关注
    function is_indexed($arr) {
      return (bool) count( array_filter( array_keys($arr), 'is_string') );
    }
    
    评论

报告相同问题?