In other programming languages the definition of arrays is something which can hold similar kind of elements. For example if I declare something like int i[]
it will store integers, but in PHP a single array seems to be holding strings and numbers together.
在其他编程语言中,数组的定义可以包含类似的元素。例如,如果我声明像int I[]这样的东西,它将存储整数,但是在PHP中,一个数组似乎将字符串和数字放在一起。
Will the number/integer be treated as string in such type of array in PHP?
在PHP中这种类型的数组中,数字/整数会被当作字符串来处理吗?
4 个解决方案
#1
9
According to the PHP manual you can indeed store heterogeneous types inside a PHP "array" - scroll down to example 3.
根据PHP手册,您确实可以在PHP“数组”中存储异构类型——向下滚动到示例3。
Note that even though the example is about keys being ints or strings, the values assigned in the example are also both ints and strings, demonstrating that it is possible to store heterogeneous types.
请注意,尽管示例中的键是int或string,但示例中分配的值也是int和string,这表明可以存储异构类型。
Be aware that in the case of different-typed keys there is automatic casting involved so you may have surprising results in the case where e.g. a string contains a valid decimal representation.
请注意,在不同类型的键的情况下,会涉及到自动强制转换,因此在字符串包含有效的十进制表示的情况下,您可能会得到令人惊讶的结果。
#2
2
In PHP arrays aren't even arrays, they're ordered hash-tables.
在PHP数组中,它们不是数组,而是有序的散列表。
#3
1
Not going to put oil on the fire of the PHP Arrays are no arrays here… But yes, you can put different variable types (string, int, …) together in a PHP thing called Array.
PHP数组中没有数组,但可以将不同的变量类型(string, int,…)放在一起,这就是所谓的数组。
#4
0
You can store anything you want in an array.
您可以在数组中存储任何您想要的内容。
Will the number/integer be treated as string in such type of array in PHP?
在PHP中这种类型的数组中,数字/整数会被当作字符串来处理吗?
Not upon storing it. However, when you use a value as such, PHP will convert it. The usage of a value determines its interpretation. (Attention, the key is converted upon storing, however, if it is considered numerical)
不是存储它。但是,当您使用这样的值时,PHP将对其进行转换。值的用法决定了它的解释。(注意,如果密钥是数值的,则在存储时进行转换)
#1
9
According to the PHP manual you can indeed store heterogeneous types inside a PHP "array" - scroll down to example 3.
根据PHP手册,您确实可以在PHP“数组”中存储异构类型——向下滚动到示例3。
Note that even though the example is about keys being ints or strings, the values assigned in the example are also both ints and strings, demonstrating that it is possible to store heterogeneous types.
请注意,尽管示例中的键是int或string,但示例中分配的值也是int和string,这表明可以存储异构类型。
Be aware that in the case of different-typed keys there is automatic casting involved so you may have surprising results in the case where e.g. a string contains a valid decimal representation.
请注意,在不同类型的键的情况下,会涉及到自动强制转换,因此在字符串包含有效的十进制表示的情况下,您可能会得到令人惊讶的结果。
#2
2
In PHP arrays aren't even arrays, they're ordered hash-tables.
在PHP数组中,它们不是数组,而是有序的散列表。
#3
1
Not going to put oil on the fire of the PHP Arrays are no arrays here… But yes, you can put different variable types (string, int, …) together in a PHP thing called Array.
PHP数组中没有数组,但可以将不同的变量类型(string, int,…)放在一起,这就是所谓的数组。
#4
0
You can store anything you want in an array.
您可以在数组中存储任何您想要的内容。
Will the number/integer be treated as string in such type of array in PHP?
在PHP中这种类型的数组中,数字/整数会被当作字符串来处理吗?
Not upon storing it. However, when you use a value as such, PHP will convert it. The usage of a value determines its interpretation. (Attention, the key is converted upon storing, however, if it is considered numerical)
不是存储它。但是,当您使用这样的值时,PHP将对其进行转换。值的用法决定了它的解释。(注意,如果密钥是数值的,则在存储时进行转换)