[英]how to pass multidimensional array (having numeric keys ) from controller to template?
[英]How to pass multidimensional array from controller to view page and how to access it
任何人都可以告訴我如何從控制器訪問傳遞多維對象數組以查看頁面以及如何從查看頁面訪問這些數據。 我的要求看起來像這樣
我有我的第一個數組
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);
}
而且我還需要像這樣將值附加到這個數組
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);
}
}
$this->photos_m->get_friend_photos_by_date($obj->relation_id, str_pad($i, 2, '0', STR_PAD_LEFT) , $month, $year);
是一個對象數組。
請注意,我必須將數據存儲在這樣的多維數組中
$search_result[0][1],$search_result[0][1] .... $search_result[n][n]
請檢查我粘貼在上面的代碼,如果我做錯了什么,請指導我。 如果正確,請告訴我如何從視圖頁面訪問該數據?
由於我無法發表評論,我將發布答案並對其進行編輯:)
您正在使用 codeigniter,對嗎?
因此,如果您將數組(在您的Controller 中)傳遞給您的視圖,例如:
$data['results'] = $search_result;
$this->load->view('your_view', $data);
然后您可以像這樣訪問您的多維數組(在您的View 中):
<?php foreach($results as $result):
print_r($result);
endforeach:?>
如果這不起作用:您是否檢查過在執行過程中是否有任何輸出
foreach($friends as $friend=>$obj){
我的意思是,你檢查過 $friend 和 $obj 是否真的有值嗎?
更重要的是:你有沒有print_r($search_result);
在你的循環之后? 只是看看你的數組中是否真的有任何值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.