如何将多维数组传递给在codeIgniter视图 [英] How to pass a multi dimensional array to a view in CodeIgniter

查看:242
本文介绍了如何将多维数组传递给在codeIgniter视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是真的在做我的螺母。
我传递一个多维数组到一个视图是这样的:

this is really doing my nut in. I'm passing a multidimensional array to a view like this:

$res = $this->deliciouslib->getRecentPosts();

(你可以看到它的美味的API,我摆弄)

(as you can see its the delicious API I'm playing with)

$结果是一个数组,的print_r($结果)给这样的:

$result is an array and print_r($result) give something like this:

我的问题是如何通过这个视图中的迭代!我一直在尝试这样的东西,

My problem is how to iterate through this in the view! I have been trying stuff like this,

$result = $this->deliciouslib->getRecentPosts();
$i=0;

foreach($result as $value)
{                   
     $val = 'val'.$i;           
     $data[$val]=$value;
     $i++;          
}

$this->load->view('delicious_view',$data);
return true;

,然后在视图类似...

And then, in the view something like...

foreach ($val0 as $value)
{
   echo $value."<br>";
}

显然,这是不行的,因为我需要所有的$ VAL(我)!结果
男人我BrainCramp!我可能跳舞周围的答案,像篮球圆箍,但我仍然完全stumnped。任何想法如何,我可以遍历整个数组throught,将是最有帮助的......

Obviously this doesn't work, as I need all of "$val(i)"!.
Man I got BrainCramp!! I'm probably dancing around the answer, like a basketball round the hoop, but I'm totally stumnped nonetheless. Any Ideas how I can iterated throught the entire array, would be most helpful....

推荐答案

在codeIgniter,当你传递一个数组给视图中的每个键设置一个简单的变量:

In CodeIgniter, when you pass an array to the view every key is set a simple variable:

 $data = array('foo' => 'bar');
 $this->load->view('myview', $data)

 // In your view
 echo $foo; // Will output "bar"

所以,如果你想传递一个数组,只需设置一个值作为一个数组:

So if you want to pass an array, just set a value as an array:

 $data = array('foo' => array('bar1', 'bar2') );
 $this->load->view('myview', $data)

 // In your view
 foreach($foo as $bar) {
   echo $bar . " "; // Will output "bar1 bar2 "
 }

这篇关于如何将多维数组传递给在codeIgniter视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆