处理键值中包含句点的数组

发布于 2024-10-09 12:36:18 字数 359 浏览 0 评论 0原文

我正在从数组中获取数据。由于某种原因,该数组具有像 [3.3] 这样的键值,我无法从中检索数据。

我有这个数组 [3.3] =>名字 [3.6] =>姓氏[2] => [电子邮件受保护]

当我尝试调用 $array[3.3] 时,它返回 null,但是当我调用 $array[2] 时,我会收到电子邮件。有什么想法吗?

I'm getting data from an array. For some reason the array has key values like [3.3] which I'm having trouble retrieving data from.

I have this array [3.3] => First Name [3.6] => Last Name[2] => [email protected].

When I try to call $array[3.3] it returns null, but when I call $array[2] I am given the e-mail. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

还不是爱你 2024-10-16 12:36:18

引用键值时使用单引号(基本上将其视为字符串,PHP 可能就是这样做的)

echo $array['3.3'];

Use single quotes when referencing the key value (basically treat it like a string, that's what PHP is probably doing)

echo $array['3.3'];
混吃等死 2024-10-16 12:36:18

来自 php 手册:

key 中的浮点数被截断为整数。

所以你试图获取不存在的 $array[3] ,所以你得到 Null

From php manual :

Floats in key are truncated to integer.

So you're trying to get $array[3] which does not exist, so you get Null

暮年 2024-10-16 12:36:18

键可以是整数或字符串。如果一个键是整数的标准表示,它将被这样解释(即“8”将被解释为8,而“08”将被解释为“08”)。 key 中的浮点数被截断为整数。索引数组和关联数组类型在 PHP 中是相同的类型,都可以包含整数和字符串索引。

http://php.net/manual/en/language.types.array.php 浮点数总是会被截断为整数(例如 3.3 总是被数组解释为 3) 我想知道你的数组是否期望一个字符串而不是浮点数。您是否尝试过 $array["3.3"] 而不是 $array[3.3] ?

A key may be either an integer or a string. If a key is the standard representation of an integer, it will be interpreted as such (i.e. "8" will be interpreted as 8, while "08" will be interpreted as "08"). Floats in key are truncated to integer. The indexed and associative array types are the same type in PHP, which can both contain integer and string indices.

Since a float would always get truncated as an integer (e.g. 3.3 would always be interpreted by the array as 3) I wonder if your array is expecting a String not a float. Have you tried $array["3.3"] instead of $array[3.3]?

云雾 2024-10-16 12:36:18

我猜它与 PHP autocasting 3.3 有关 =>浮动

尝试 $array['3.3']

I guess it has something todo with the PHP autocasting 3.3 => float

try $array['3.3']

仙气飘飘 2024-10-16 12:36:18

键中的

浮点数数字字符串被截断为整数

所以输出这段代码:

$array = [1 => "a", "1" => "b", 1.5 => "c", true => "d"];
print_r($array);

将是:

Array
(
    [1] => d
)

Floats and numeric string in key are truncated to integer.

So output this code:

$array = [1 => "a", "1" => "b", 1.5 => "c", true => "d"];
print_r($array);

would be:

Array
(
    [1] => d
)
一个人的旅程 2024-10-16 12:36:18

当使用浮动键将元素添加到数组中时,我遇到了类似的问题 - PHP 正在覆盖现有值(键 1.2 被 1.5 等覆盖)。

基于这个SO线程,我将转换键添加到字符串中:

$options[(string)$value] = new TpValueModelOption($value, $label);

I had a similar problem when adding elements into array using float keys - PHP was overwriting existing values (key 1.2 was overwritten by 1.5 etc.).

Based on this SO thread I add cast key to string:

$options[(string)$value] = new TpValueModelOption($value, $label);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文