Home  >  Article  >  Backend Development  >  神秘的数组是怎么来的

神秘的数组是怎么来的

WBOY
WBOYOriginal
2016-06-23 13:33:07734browse

先说说大概:这是一个模板中输出页面的问题。它是用一个数组就输出了一大坨的信息,里面有文字也有图片。
让人不解的是,它只用了一个句子就办了。

为了便于大家分析,我先列表在模板中的句子




这一句话就输出了N许多文字加图片展示在首页。然后我又找到了调用方法句子的方法
是下面这一大坨(当然限于篇幅我不可能全写出来,因为太长,只是把这关键的展示出来,相信高手一看就明白)
                //板块信息
$model_web_config = Model('web_config');
$web_html = $model_web_config->getWebHtml('index');
Tpl::output('web_html',$web_html);

关于getWebHtml这个方法如下:
/**
 * 模块html信息
 *
 */
public function getWebHtml($web_page = 'index',$update_all = 0){
$web_array = array();
$web_list = $this->getWebList(array('web_show'=>1,'web_page'=> array('like',$web_page.'%')));
if(!empty($web_list) && is_array($web_list)) {
foreach($web_list as $k => $v){
    $key = $v['web_page'];
if ($update_all == 1 || empty($v['web_html'])) {//强制更新或内容为空时查询数据库
$web_array[$key] .= $this->updateWebHtml($v['web_id'],$v['style_name']);
} else {
$web_array[$key] .= $v['web_html'];
}
}
}
return $web_array;
}
好了,我的问题来了,
$output['web_html']['index'];是怎么来的,怎么['web_html']和['index']就能搞成数组呢,道理何在?


回复讨论(解决方案)

你看看 getWebList 方法是怎么写的
或打印出 $web_list
就知道了

你看看 getWebList 方法是怎么写的
或打印出 $web_list
就知道了


这个是$web_list

/**
 * 读取记录列表
 *
 * @param
 * @return array 数组格式的返回结果
 */
public function getWebList($condition = array('web_page' => 'index'),$page = ''){
$result = $this->table('web')->where($condition)->order('web_sort')->page($page)->select();
return $result;
}

我打印了一下$web_list数组是这样形式:
Array ( [0] => Array ( [web_id] => 1 [web_name] => 1F [style_name] => red [web_page] => index [update_time] => 1433752194 [web_sort] => 1 [web_show] => 1 [web_html] =>

我是这样想的:即然$output['web_html']['index']是一个数组,那么,它肯定是一个二维数组,并且能打印出来二维的键值关系来。
但是这个还真打不出来,挺怪的。

你的 $web_list 有这样一项 [web_page] => index
在你的代码
foreach($web_list as $k => $v){
     $key = $v['web_page']; //此时的 $key 的值不就是 index 了吗?
   if ($update_all == 1 || empty($v['web_html'])) {//强制更新或内容为空时查询数据库
      $web_array[$key] .= $this->updateWebHtml($v['web_id'],$v['style_name']);
   } else {
      $web_array[$key] .= $v['web_html'];
   }
}
那么对 $web_array[$key] 赋值,不就是对 $web_array['index'] 赋值吗?


*****************


是不是这个在起作用,就类似于模板标签什么的。你看下它模板系统里有没有针对这个标签的特殊处理呢。。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn