【发布时间】:2011-09-28 15:23:48
【问题描述】:
我有一个 person 数组,其中的键为“n”。 'n' 的值是一个数组,键 'l'('last' 的缩写)具有我想要获取的值,即姓氏。
出于好奇的编程原因,我将姓氏的“地址”作为字符串。
我该怎么做?
我试过 eval 等等。
对不起,我没有说清楚!
我想要的是使用变量 $test 来获取它。
例如类似:
$person[$test]
(这显然行不通,但什么行?)
$test 并不总是“两深”。例如。有时会是['a']['residential']['street']
<?php
$test = "['n']['l']";
echo "I've got a string with the array index ".$test. "\n";
$person = array("n"=>array('l'=>'SMITH'));
echo 'the array, $person, is this: '."\n";
print_r($person);
echo "What I want is ".$person['n']['l']. "\n";
?>
输出:
我有一个数组索引为 ['n']['l'] 的字符串 数组 $person 是这样的: 大批 ( [n] => 数组 ( [l] => 史密斯 ) )我想要的是史密斯
【问题讨论】:
-
我不完全确定您的问题是什么。您表明您可以访问 $person['n']['l'],但似乎这是您的问题。
-
你已经这样做了,怎么了?
-
我不清楚你的问题,但我绝对不想在回显中显示数组的内容,但你必须使用 print_r() php.net/manual/en/function.print-r.php
-
我猜 op 想使用
$test变量来获取数组位置,而不是在echo ... $person['n']['l']部分使用静态['n']['l']。 -
删除了 mongodb 标签,因为它与这个问题无关(即使那是数据的来源)。
标签: php arrays string indexing