Home > Backend Development > PHP Tutorial > 在获取的结果中增加一个对象名称

在获取的结果中增加一个对象名称

WBOY
Release: 2016-06-06 20:09:07
Original
918 people have browsed it

目前获取到的结果是 如示例1 结构 ,
表中没有value字段
$tree = M('Kind')->where(array('status'=>1))->field('id, title as name')->select();
$this->ajaxReturn($tree);
想要获取 如示例2 的结构,有方便的方法吗?
框架是 thinkphp

<code>//示例1
[
  {"id": "4","name": "服饰鞋类箱包"},
  {"id": "8","name": "母婴"}
]</code>
Copy after login
Copy after login
<code>//示例2
[
    {"value":{"id":4,"name":"服饰鞋类箱包" }},
    {"value":{ "id":8,"name":"母婴"}}
]</code>
Copy after login
Copy after login

回复内容:

目前获取到的结果是 如示例1 结构 ,
表中没有value字段
$tree = M('Kind')->where(array('status'=>1))->field('id, title as name')->select();
$this->ajaxReturn($tree);
想要获取 如示例2 的结构,有方便的方法吗?
框架是 thinkphp

<code>//示例1
[
  {"id": "4","name": "服饰鞋类箱包"},
  {"id": "8","name": "母婴"}
]</code>
Copy after login
Copy after login
<code>//示例2
[
    {"value":{"id":4,"name":"服饰鞋类箱包" }},
    {"value":{ "id":8,"name":"母婴"}}
]</code>
Copy after login
Copy after login

<code>$tree = M('Kind')->where(array('status'=>1))->field('id, title as name')->select();
$return_data = array();
foreach ($tree as $val){
    $return_data[] = array("value" => $val);
}
$this->ajaxReturn($return_data);</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template