php 框架,数据在 var_dump 中有效,但在 echo 中无效

标签 php codeigniter

我现在很困惑,我正在使用 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/

相关文章:

php - HTTPS 页面上的错误 500

php - 从像素数组创建图像服务器端

php - 去掉()前后的 ' '

php - MySQL 中的负索引长度

javascript - HTML Action ID 获取方法

php - 比较两个 MySQL 表以填充下拉列表

php - codeigniter where() postgresql

php - 从同一个表中获取不同条件的数据

javascript - 使用 angularjs+codeigniter 成功添加用户时在 html 中显示消息

php - 使用 CodeIgniter 的通配符子域路由