通过键获取数组值

发布于 2024-11-24 16:24:50 字数 634 浏览 3 评论 0原文

首先我想说的是,我已经对这个解决方案进行了很多研究,但我发现的只是相反的。也就是说,我试图弄清楚如何获取附加到数组中某个键的值,但我所能找到的只是通过使用该值来获取键。

因为我找不到这个,所以我很想相信这是一个非常简单的问题,但我无法弄清楚。我查看了数组文档,发现的只是一种使用“current”的方法,这对我来说不起作用。

假设我有一个具有不同键的数组,如下所示:

$naEUS["A.1.5.3"] = array( 1000 , 879 , 192 );
$naEUS["A.1.5.4"] = array( 1012 , 922 , 456 );

我的目标是做类似的事情:

$var = "A.1.5.3";
$goal = $naEUS[$var];

我确实希望这不是愚蠢的事情,因为我今天已经有了其中一个。请记住,如果是的话,就没有愚蠢的问题,只有不问的愚蠢的人。我只是想在这里学习。

如果可能的话,我试图避免任何循环。

Edit1:显然这就是你的做法,我这边一定是有其他问题。感谢您的所有帮助,当我弄清楚时我会发布我的修复程序。

Edit2:这个例子比我的实际代码稍微简单一些,我只是将 $var 分配给了更上面的错误。

I would like to start off by saying that I have looked a good amount for this solution, but all I find is the opposite. That is, I'm trying to figure out how to get the value attached to a certain key in the array, but all I can find is grabbing the key by using the value.

Because I can't find this, I am very tempted to believe that it is a very simple problem, yet I cannot figure it out. I have looked at the array documentation, and all I've found is a method using "current," which will not work for me here.

Let's say I have an array of arrays which have distinct keys, like this:

$naEUS["A.1.5.3"] = array( 1000 , 879 , 192 );
$naEUS["A.1.5.4"] = array( 1012 , 922 , 456 );

What my goal is, is to do something like:

$var = "A.1.5.3";
$goal = $naEUS[$var];

I do hope it's not something silly, because I've already had one of those today. And please try to remember that, if it is, there is no such thing as a stupid question, just stupid people who don't ask. I'm just trying to learn here.

I'm trying to avoid any loops, if possible.

Edit1: Evidently this is how you do it, must be something else wrong on my end. Thanks for all the help and I will post my fix when I figure it out.

Edit2: This example is a little less complex than my actual code, I was just assigning $var to something wrong a bit further up.

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

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

发布评论

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

评论(1

锦欢 2024-12-01 16:24:50

运行

$naEUS["A.1.5.3"] = array( 1000 , 879 , 192 );
$naEUS["A.1.5.4"] = array( 1012 , 922 , 456 );

$var = "A.1.5.3";
$goal = $naEUS[$var];

var_dump($goal);

返回:

array(3) { [0]=> int(1000) [1]=> int(879) [2]=> int(192) }

您在寻找什么?

Running

$naEUS["A.1.5.3"] = array( 1000 , 879 , 192 );
$naEUS["A.1.5.4"] = array( 1012 , 922 , 456 );

$var = "A.1.5.3";
$goal = $naEUS[$var];

var_dump($goal);

returns:

array(3) { [0]=> int(1000) [1]=> int(879) [2]=> int(192) }

What were you looking for?

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