[英]How to print the passed multidimensional array from controller to view in codeigniter?
I am having a problem in accessing/creating a foreach statement when using a multidimensional array. 使用多维数组时,我在访问/创建foreach语句时遇到问题。
In my controller I have the following: 在我的控制器中,我有以下内容:
$data = array();
$temp = array();
...
some codes here
...
array_push($data, $temp);
$this->load->view('view', $data);
The $data array contains something like this: $ data数组包含以下内容:
Array(
[0]=>
Array(
[information]=>
Array(
[name]=>John Doe
[age] =>21
)
[data]=>
Array(
[earnings]=>
Array(
[salary]=>21000
[bonus]=>1000
)
[deductions]=>
Array(
[tax]=>1000
[loan]=>0
)
)
)
[1]=>
Array(
[information]=>
Array(
[name]=>George Read
[age] =>23
)
[data]=>
Array(
[earnings]=>
Array(
[salary]=>20000
[bonus]=>1000
)
[deductions]=>
Array(
[tax]=>990
[loan]=>200
)
)
)
) )
What should I do in view to be able to print/echo the data? 我应该怎么做才能打印/回显数据? Your help will be highly appreciated. 非常感谢您的帮助。
If you're trying to access a variable that's been assigned to $data
in the controller, you use the index of the array as a variable. 如果尝试访问已在控制器中分配给$data
的变量,则可以使用数组的索引作为变量。 So if you wanted to echo out the variable in the view, you'd use 因此,如果您想在视图中回显该变量,则可以使用
echo $0['information']['name'];
If you wanted to use a Foreach loop, you could use a nested foreach loop. 如果要使用Foreach循环,则可以使用嵌套的foreach循环。 But in that case, it would be better to assign the $temp
array to a specific variable in the controller 但是在那种情况下,最好将$temp
数组分配给控制器中的特定变量
Controller 调节器
$data['temp_array'] = $temp;
Then the foreach loop would look like this: 然后,foreach循环将如下所示:
View 视图
foreach ($temp_array as $array_name => $array) {
echo $array_name;
foreach ($array as $key => $value) {
echo $key ': ' . $value;
}
}
See this page for info on looping through multidimensional arrays. 有关遍历多维数组的信息,请参见此页面 。
First of all define your array in the controller file something like this: 首先,在控制器文件中定义数组,如下所示:
$array = array(
0 => array(
"foo" => "bar",
42 => 24,
"multi" => array(
"dimensional" => array(
"array" => "foo"
)
)
),
1 => array(
"foo" => "bar",
42 => 24,
"multi" => array(
"dimensional" => array(
"array" => "foo"
)
)
)
);
Now the question is, how to access [0]or[1]! 现在的问题是,如何访问[0]或[1]! to do that assign this array to $data['arbitrary_variable'] 为此,将该数组分配给$ data ['arbitrary_variable']
In the view file you could directly use this $arbitrary_variable as array. 在视图文件中,您可以直接使用此$ arbitrary_variable作为数组。
Controller Code: 控制器代码:
public function index() {
$data['magic'] = $array = array(
0 => array(
"foo" => "bar",
42 => 24,
"multi" => array(
"dimensional" => array(
"array" => "foo"
)
)
),
1 => array(
"foo" => "bar",
42 => 24,
"multi" => array(
"dimensional" => array(
"array" => "foo"
)
)
)
);
$this->load->view('test',$data);
}
View Code: 查看代码:
<?php
print_r($magic);
echo '<br/>';
echo $magic[0]['foo'];
?>
The idea here is to put your desired array into yet another array, ie not to assign it directly to $data but to a key of data 这里的想法是将所需的数组放入另一个数组,即不直接将其分配给$ data而是分配给data的键
You should do the following changes: 您应该进行以下更改:
1) You should try to add an element to the data array you are passing to the view, so you can access the element and loop it. 1)您应该尝试将元素添加到传递给视图的数据数组中,以便可以访问该元素并将其循环。
$data = array();
$temp = array();
//some codes here
array_push($data['anametoaccess'][], $temp); //this will give us a element name to access and loop the array
$this->load->view('view', $data);
2) This is the code for the view: 2)这是视图的代码:
foreach( $anametoaccess as $key => $each ){
$information = $each['information']; //now you have the information array in $informationarray
$earnings = $each['data']['earnings'];
$deductions = $each['data']['deductions'];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.