在PHP关联数组的项目不被访问的数值(即通过索引)? [英] Can items in PHP associative arrays not be accessed numerically (i.e. by index)?
问题描述
我想知道为什么,我有一个查询字符串页面上,
的code:
I'm trying to understand why, on my page with a query string, the code:
echo "Item count = " . count($_GET);
echo "First item = " . $_GET[0];
结果:搜索
项目数量= 3
第一项=
Item count = 3 First item =
是PHP关联数组从数字数组不同,使自己的项目不能按索引访问?谢谢 -
Are PHP associative arrays distinct from numeric arrays, so that their items cannot be accessed by index? Thanks-
推荐答案
他们不能。当您通过它的主键/索引下标值,它必须完全匹配。
They can not. When you subscript a value by its key/index, it must match exactly.
如果你真的想使用数字键,您可以使用 array_values()
上 $ _ GET
,但是你将失去所有关于密钥的信息。你也可以使用 array_keys()
领取钥匙和数值指标。
If you really wanted to use numeric keys, you could use array_values()
on $_GET
, but you will lose all the information about the keys. You could also use array_keys()
to get the keys with numerical indexes.
另外,作为菲尔提到,你可以的 复位()
内部指针拿到第一。您还可以得到最后用 端()
。您也可以弹出或 array_pop转移()
和 array_shift()
,双方一旦修改该数组将返回值。
Alternatively, as Phil mentions, you can reset()
the internal pointer to get the first. You can also get the last with end()
. You can also pop or shift with array_pop()
and array_shift()
, both which will return the value once the array is modified.
这篇关于在PHP关联数组的项目不被访问的数值(即通过索引)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!