【发布时间】:2014-09-14 00:21:16
【问题描述】:
我的代码中有一个奇怪的行为。
下划线模板:
<% _.each(pinnwand, function(item,key,list) { %>
<strong>Item: <%= item.name %></strong>
<% }); %>
<% _.each(pinnwand2, function(item,key,list) { %>
<strong>Item: <%= item.name %></strong>
<% }); %>
对于数组“pinnwand”和“pinnwand2”。
数组:
function getPinnwandData() {
header('Content-Type: application/json');
$array = $this->Pinnwand_model->getAllPinnwand($this->input->post('user_id'),"json");
$data['pinnwand'] = array();
foreach($array as $a) {
$data['pinnwand'][] = array("name" => strtolower($a['name']), "id" => $a['id']);
}
$data['pinnwand2'] = array(array('name' => 'MettWand',"id" => 1),array('name' => 'wurst2',"id" => 2));
echo json_encode($data);
}
奇怪的是,“pinnwand2”正在显示,而“pinnwand”没有。
控制台日志显示:
Object {pinnwand: Array[0], pinnwand2: Array[2]}
而且数据库中肯定有一些“pinnwand”的记录。
这是“邮递员”的 json 输出:
{
"pinnwand": [
{
"name": "wurst wand",
"id": "1"
},
{
"name": "schinken wand",
"id": "2"
},
{
"name": "mett wand ",
"id": "3"
}
],
"pinnwand2": [
{
"name": "MettWand",
"id": 1
},
{
"name": "wurst2",
"id": 2
}
]
}
我错过了什么?!谢谢!
编辑:
getAllPinnwand 方法:
function getAllPinnwand($userId, $main = "all") {
if($main == "json") {
$query = $this->db->get_where('pinnwand', array('user_id' => $userId));
return $query->result_array();
} else {
$query = $this->db->get_where('pinnwand', array('user_id' => $userId));
$result = $query->result();
foreach($result as $r) {
$query1 = $this->db->get_where('pinnwand_content', array('pinnwand_id' => $r->id));
$r->content = $query1->result();
}
return $result;
}
}
【问题讨论】:
标签: php arrays json codeigniter underscore.js