使用数字索引从控制器传递数据,查看 [英] Using numeric indexes to pass data from controller to view
问题描述
我有一个简单的问题。让我来解释结果
我们用它来从控制器传递数据,查看
I have a simple question. Let me explain
We use this to pass data from controller to view
function index(){
$data['title'] = 'This is title';
$data['message'] = 'This is message';
$this->load->view('test',$data);
}
下面我们使用关联数组来传递数据结果
而现在这又功能及使用方法,而不是关联数组索引数组
Here we are using Associative Array to pass data
And now this function again and use indexed array instead of Associative Array
function index(){
$data[] = 'This is title';
$data[] = 'This is message';
$this->load->view('test',$data);
}
和现在查看这不起作用。
And now in View this does not work.
echo $data[0];
echo '<br>';
echo $data[1];
我只想知道为什么这是行不通的。而且用户指南中,我从来没有读到关联数组是必要的。
i only want to know if why this does not work. And in the user guide i never read that associative array is necessary.
推荐答案
解析时,视图数据转换成变量。什么的一个类似的结果PHP提取物()
函数提供。例如:
The view data are converted into variables when parsed. A similar result of what extract()
function of PHP gives. For example:
$data['title'] = 'This is the title';
将直接访问为 $标题
不可以 $数据['标题']
。事实上,如果你看看源代码,你会发现它的用途提取物()
和类似的转换发生在你的情况,但由于变量 $ 1,0
和 $ 1
是无效的,使他们无法使用。
will be accessible directly as $title
not $data['title']
. In fact, if you look at the sources, you will find it does uses extract()
and similar conversion happens on your case to, but since variable $0
and $1
are invalid so they are not available.
坚持字符串索引。如果这是不是一种选择,那么你可能要preFIX一些文本之前,如:
Stick to string indexing. If that is not an option, then you might want to prefix something before the texts like:
$data['d0'] = 'This is the title';
在这里阅读手册 的引用它。然而,你可以传递一个数组,而不是一个字符串,给你想要什么确切的结果。
Read the manual here its quoted. However, you can pass an array instead of a string and giving the exact result of what you want.
$data['data'] = array('This is the title', 'This is the description');
现在,这一点,你就可以这样使用访问 $ DATA [0]
和 $数据[1]
。
Now, this you will be access using $data[0]
and $data[1]
.
这篇关于使用数字索引从控制器传递数据,查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!