如何测试数组是“索引”与“关联” [英] How to test that an array is 'indexed' vs 'associative'

查看:59
本文介绍了如何测试数组是“索引”与“关联”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类构造函数,它接受一个数组作为唯一的参数。

catch是数组必须是''整数索引''数组,而不是

''associative''数组,因为索引位置有意义。


如何在构造函数中测试这个,而不必遍历

整个数组并检查以确保键是数字和顺序的?


Daniel Klein

Cuyahoga Falls,OH

解决方案

我的意见是不检查它。只需使用

" array_values"


在2008年5月31日星期六15:57:07 +0200,Daniel Klein

< da ***** @ featherbrain.netwrote:


我有一个类构造函数,它接受一个数组作为唯一参数。



catch是数组必须是''整数索引''数组,而不是

''associative''数组,因为索引位置有意义。


如何在构造函数中测试这个,而不必遍历

整个数组并检查以确保键是数字和

顺序?



这样的东西可能吗?

array_keys(


array)=== array_flip(array_keys (

I have a class constructor that accepts an array as the only argument. The
catch is that the array MUST be an ''integer-indexed'' array, not an
''associative'' array, because the index position has meaning.

How can I test for this in the constructor without having to traverse the
entire array and checking to make sure the keys are numeric and sequential?

Daniel Klein
Cuyahoga Falls, OH

解决方案

my opinion is dont check it. just retreve the values with
"array_values"


On Sat, 31 May 2008 15:57:07 +0200, Daniel Klein
<da*****@featherbrain.netwrote:

I have a class constructor that accepts an array as the only argument.
The
catch is that the array MUST be an ''integer-indexed'' array, not an
''associative'' array, because the index position has meaning.

How can I test for this in the constructor without having to traverse the
entire array and checking to make sure the keys are numeric and
sequential?

Something like this maybe?
array_keys(


array) === array_flip(array_keys(


这篇关于如何测试数组是“索引”与“关联”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆