PHP关联数组 - 如何看待整数作为字符串 [英] PHP associative arrays - how to treat integer as string

查看:122
本文介绍了PHP关联数组 - 如何看待整数作为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的关联数组。

I have a simple associative array.

$a = array("a"=>"b", "c"=>"d");

我要检查如果键1的存在数组中,例如

I want to check if the key "1" exists in the array, e.g.

isset($a["1"]);

这串被视为一个整数,这样

This string is being treated as an integer, so that

echo $a["1"]; //prints "d"

我如何得到它把它作为一个字符串?

How do I get it to treat it as a string?

我不希望使用array_key_exists或in_array因为我的基准测试表明使用isset会快很多。

I don't want to use array_key_exists or in_array because my benchmarking shows isset will be a lot faster.

推荐答案

它不会出现,你可以做你想做的事情。从<一个href=\"http://us.php.net/manual/en/language.types.array.php\">http://us.php.net/manual/en/language.types.array.php:

It doesn't appear that you can do what you want to do. from http://us.php.net/manual/en/language.types.array.php:

一个键可以是整数或字符串。如果一个密钥是一个整数的标准重新presentation,这将是PTED如(例如8将PTED 8间$ P $间$ P $,而08,将除preTED为08)。

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").

您可能会需要使用prefixing所有钥匙的东西的Fosco的建议。如果您使用相同的preFIX上的每个键,那么它不,如果你分析可能有文字和数字的文本关系 - 把同样的preFIX上的一切,无论

You'll probably have to use Fosco's suggestion of prefixing all your keys with something. If you use the same prefix on every key, then it doesn't matter if you're parsing a text that might have words and numbers - put the same prefix on everything regardless.

这篇关于PHP关联数组 - 如何看待整数作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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