简体   繁体   English

如何将多维数组从控制器传递到视图页面以及如何访问它

[英]How to pass multidimensional array from controller to view page and how to access it

Can anybody tell me like how can i access pass multidimensional object array from a controller to view page and how can i access those data from the view page.任何人都可以告诉我如何从控制器访问传递多维对象数组以查看页面以及如何从查看页面访问这些数据。 My requirement look like this我的要求看起来像这样

i have my first array我有我的第一个数组

for($i=1; $i<=$day;$i++){
    $search_result[$i][1] = $this->photos_m->get_user_photos_by_date($this->user->_id, str_pad($i, 2, '0', STR_PAD_LEFT) , $month, $year);
} 

and also i need to append the values to this array like this而且我还需要像这样将值附加到这个数组

foreach($friends as $friend=>$obj){   
    $friendCount++;
    for($i=1; $i<=$day;$i++){                                      
        $search_result['results'][$j] = $this->photos_m->get_friend_photos_by_date($obj->relation_id, str_pad($i, 2, '0', STR_PAD_LEFT) , $month, $year);
    }
}

the result of $this->photos_m->get_friend_photos_by_date($obj->relation_id, str_pad($i, 2, '0', STR_PAD_LEFT) , $month, $year); $this->photos_m->get_friend_photos_by_date($obj->relation_id, str_pad($i, 2, '0', STR_PAD_LEFT) , $month, $year); is an object array.是一个对象数组。

Please note that i have to store the data in a multidimensional array like this请注意,我必须将数据存储在这样的多维数组中

$search_result[0][1],$search_result[0][1] .... $search_result[n][n]

please check my code that i pasted above and please guide me if i have done anything wrong.请检查我粘贴在上面的代码,如果我做错了什么,请指导我。 If it is right please tell me how to access that data from the view page?如果正确,请告诉我如何从视图页面访问该数据?

Since I can't comment I'll post an answer and edit it :)由于我无法发表评论,我将发布答案并对其进行编辑:)
You're using codeigniter, right?您正在使用 codeigniter,对吗?

So if you pass an array (in your Controller ) to your view like:因此,如果您将数组(在您的Controller 中)传递给您的视图,例如:

$data['results'] = $search_result;
$this->load->view('your_view', $data);

You can then access your multi dimensional array(in your View ) like this:然后您可以像这样访问您的多维数组(在您的View 中):

<?php foreach($results as $result):
print_r($result); 
endforeach:?>

If that doesn't work: Did you check if you got any output while doing如果这不起作用:您是否检查过在执行过程中是否有任何输出

foreach($friends as $friend=>$obj){   

I mean, did you check if $friend and $obj actually have a value?我的意思是,你检查过 $friend 和 $obj 是否真的有值吗?
And more important: Did you print_r($search_result);更重要的是:你有没有print_r($search_result); after your loop?在你的循环之后? Just so see if you actually got any values in your array.只是看看你的数组中是否真的有任何值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM