【发布时间】:2013-04-30 01:25:35
【问题描述】:
如何使用 CodeIgniter 遍历并显示以下 JSON 中的名称?
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Search extends CI_Controller {
public function index()
{
$json = '[{"name": "John Doe",
"address": "115 Dell Avenue, Somewhere",
"tel": "999-3000",
"occupation" : "Clerk"},
{"name": "Jane Doe",
"address": "19 Some Road, Somecity",
"tel": "332-3449",
"occupation": "Student"}]';
for (int $i = 0; $i < $json.length; $i++){
???
}
//$obj = json_decode($json);
//$this->load->view('search_page');
}
}
/* End of file search.php */
/* Location: ./application/controllers/search.php */
【问题讨论】:
-
$json 不是一个 json 对象,它是一个字符串。
-
+1 所以我应该先使用 json_decode($json)?
-
取消注释 json_decode 的行,然后循环遍历它返回的数组。 (这与CodeIgniter无关)
-
+1 $arr = json_decode($json, true); ?
-
实际上你的json字符串代表一个对象数组而不是一个对象本身
标签: php json codeigniter