dongwang3066
2011-08-28 11:28
浏览 83
已采纳

PHP和Codeigniter。 如何返回数组对象而不是普通数组?

In Codeigniter I am creating an array and returning it to the user. I am creating the array like this (result is the return form a DB query):

array("email" => $result)

Right now it outputs:

"email": [
    {
        "id": "629",
        "desc": "0000",
        "value_1": "0000",
        "value_2": null,
        "value_3": null,
        "value_4": null,
        "privacy": "0"
    }
]

So $result is an array that contains a single object. How can I make $result contain just the object instead? Like this:

"email": {
    "id": "628",
    "desc": "THIS IS IT",
    "value_1": "THIS IS IT2",
    "value_2": null,
    "value_3": null,
    "value_4": null,
    "privacy": "0"
}

Thankful for all input!

图片转代码服务由CSDN问答提供 功能建议

在Codeigniter中我正在创建一个数组并将其返回给用户。 我正在创建这样的数组(结果是数据库查询的返回形式):

  array(“email”=> $ result)
  <  / pre> 
 
 

现在输出:

 “email”:[
 {
“id”:“629”,
“  desc“:”0000“,
”value_1“:”0000“,
”n“value_2”:null,
“value_3”:null,
“value_4”:null,
“隐私”:“0”  
} 
] 
   
 
 

所以 $ result 是一个包含单个对象的数组。 如何让 $ result 只包含该对象? 像这样:

 “email”:{
“id”:“628”,
“desc”:“这就是它”,
“value_1”:  “这是IT2”,
“value_2”:null,
“n”value_3“:null,
”value_4“:null,
”隐私“:”0“
} 
   
 
 

感谢所有输入!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

  • douba1904 2011-08-28 11:32
    最佳回答

    Just use:

    array("email" => $result->row());
    

    See the CI documentation on queries and row():

    This function returns a single result row. If your query has more than one row, it returns only the first row. The result is returned as an object.

    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题