我现在很困惑,我正在使用 code igniter php 框架并将一个数组传递给 View ,这样我就可以从 View 中访问变量。
所以在这个 View 中,当我这样做的时候
echo var_dump($data["mykey"]);
我明白了
string '43' (length=2)
但是当我尝试
echo $data["mykey"];
我明白了
A PHP Error was encountered
Severity: Notice
Message: Undefined index: "mykey"
Line Number: 8
???
编辑(问题似乎是当代码点火器试图从 View 返回以将其作为字符串传递回 Controller 时,这是我的结论,因为如果我在 View 的末尾放置一个骰子它会起作用)
$data
数组就像这样,在我看来我尝试做 echo $data["myfield"][0]["data"]
array
'myfield' =>
array
0 =>
array
'data' => string '1'
'myfieldb' =>
array
0 =>
array
'data' => string '2'
最佳答案
在你的 Controller 中
class controller extends CI_Controller {
function hello() {
$data['world'] = 'world';
$this->load->view('test');
}
}
在你看来
<html>
<head></head>
<body>
Hello <?php echo $world ?>;
</body>
</html>
传递给 View 的数组键被转换为变量。
因此,要在您的 View 中访问 $data['mykey']
,您必须像访问 $mykey
而不是 $data['mykey']
.
<html>
<head></head>
<body>
Your key is: <?php echo $mykey ?>;
</body>
</html>
这是 user_guide
关于php 框架,数据在 var_dump 中有效,但在 echo 中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6366443/