通过键获取数组值
首先我想说的是,我已经对这个解决方案进行了很多研究,但我发现的只是相反的。也就是说,我试图弄清楚如何获取附加到数组中某个键的值,但我所能找到的只是通过使用该值来获取键。
因为我找不到这个,所以我很想相信这是一个非常简单的问题,但我无法弄清楚。我查看了数组文档,发现的只是一种使用“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
运行
返回:
您在寻找什么?
Running
returns:
What were you looking for?