如何使用CodeIgniter遍歷並顯示以下JSON中的名稱?如何通過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對象的字符串。 – Josh 2013-04-30 01:26:47
+1所以我應該先使用json_decode($ json)? – Anthony 2013-04-30 01:27:40
取消註釋表示json_decode的行並僅循環返回的數組。 (這與CodeIgniter無關) – 2013-04-30 01:27:57