gpt4 book ai didi

php - 如何循环遍历 JSON 数组

转载 作者:IT王子 更新时间:2023-10-28 23:51:15 28 4
gpt4 key购买 nike

如何使用 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 */

最佳答案

1) $json 是你需要先解码的字符串。

$json = json_decode($json);

2) 你需要遍历对象并获取它的成员

foreach($json as $obj){
echo $obj->name;
.....

}

关于php - 如何循环遍历 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16290798/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com