如何循环遍历 JSON 数组 [英] How do I loop through JSON array

查看:95
本文介绍了如何循环遍历 JSON 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 CodeIgniter 遍历和显示以下 JSON 中的名称?

How do I traverse and display the names in the following JSON using CodeIgniter?

<?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 是一个需要先解码的字符串.

1) $json is a string you need to decode it first.

$json = json_decode($json);

2) 您需要遍历对象并获取其成员

2) you need to loop through the object and get its members

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

}

这篇关于如何循环遍历 JSON 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆