如何在codeigniter中解析数组?

时间:2018-03-08 12:16:51

标签: codeigniter

我在数组结构下面如何解析这个数组并在codeigniter中显示[hits] [hits]的所有值。任何人都可以帮我解释代码吗?

  Array
(
    [took] => 2
    [timed_out] => 
    [_shards] => Array
        (
            [total] => 5
            [successful] => 5
            [skipped] => 0
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => 211
            [max_score] => 1
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => employee
                            [_type] => employee_details
                            [_id] => AULz9GEBLWX90hClBAhn
                            [_score] => 1
                            [_source] => Array
                                (
                                    [Id] => 5
                                    [First name] => c
                                    [Last name] => d
                                    [Email] => d@gmail.com
                                    [Phone] => 12
                                    [Address] => n
                                    [Password] => HARI
                                    [Status] => false
                                )

                        )

                    [1] => Array
                        (
                            [_index] => employee
                            [_type] => employee_details
                            [_id] => eM1j6mEB19ahvbtRhU6F
                            [_score] => 1
                            [_source] => Array
                                (
                                    [Id] => 
                                    [First name] => 
                                    [Last name] => 
                                    [Email] => 
                                    [Phone] => 
                                    [Address] => 
                                    [Password] => 
                                    [Status] => 
                                    [createddate] => 
                                    [Updateddate] => 
                                )

                        )

                    [2] => Array
                        (
                            [_index] => employee
                            [_type] => employee_details
                            [_id] => ec1j6mEB19ahvbtRhk5G
                            [_score] => 1
                            [_source] => Array
                                (
                                    [Id] => 
                                    [First name] => 
                                    [Last name] => 
                                    [Email] => 
                                    [Phone] => 
                                    [Address] => 
                                    [Password] => 
                                    [Status] => 
                                    [createddate] => 
                                    [Updateddate] => 
                                )

                        )

                    [3] => Array
                        (
                            [_index] => employee
                            [_type] => employee_details
                            [_id] => es1j6mEB19ahvbtRh04U
                            [_score] => 1
                            [_source] => Array
                                (
                                    [Id] => 
                                    [First name] => 
                                    [Last name] => 
                                    [Email] => 
                                    [Phone] => 
                                    [Address] => 
                                    [Password] => 
                                    [Status] => 
                                    [createddate] => 
                                    [Updateddate] => 
                                )

                        )

                    [4] => Array
                        (
                            [_index] => employee
                            [_type] => employee_details
                            [_id] => fM1j6mEB19ahvbtRiE6M
                            [_score] => 1
                            [_source] => Array
                                (
                                    [Id] => 
                                    [First name] => 
                                    [Last name] => 
                                    [Email] => 
                                    [Phone] => 
                                    [Address] => 
                                    [Password] => 
                                    [Status] => 
                                    [createddate] => 
                                    [Updateddate] => 
                                )

                        )

                    [5] => Array
                        (
                            [_index] => employee
                            [_type] => employee_details
                            [_id] => ds1j6mEB19ahvbtRg07Y
                            [_score] => 1
                            [_source] => Array
                                (
                                    [Id] => 
                                    [First name] => 
                                    [Last name] => 
                                    [Email] => 
                                    [Phone] => 
                                    [Address] => 
                                    [Password] => 
                                    [Status] => 
                                    [createddate] => 
                                    [Updateddate] => 
                                )

                        )

                    [6] => Array
                        (
                            [_index] => employee
                            [_type] => employee_details
                            [_id] => d81j6mEB19ahvbtRhE6q
                            [_score] => 1
                            [_source] => Array
                                (
                                    [Id] => 
                                    [First name] => 
                                    [Last name] => 
                                    [Email] => 
                                    [Phone] => 
                                    [Address] => 
                                    [Password] => 
                                    [Status] => 
                                    [createddate] => 
                                    [Updateddate] => 
                                )

                        )

                    [7] => Array
                        (
                            [_index] => employee
                            [_type] => employee_details
                            [_id] => hs1j6mEB19ahvbtRok5L
                            [_score] => 1
                            [_source] => Array
                                (
                                    [Id] => 
                                    [First name] => 
                                    [Last name] => 
                                    [Email] => 
                                    [Phone] => 
                                    [Address] => 
                                    [Password] => 
                                    [Status] => 
                                    [createddate] => 
                                    [Updateddate] => 
                                )

                        )

                    [8] => Array
                        (
                            [_index] => employee
                            [_type] => employee_details
                            [_id] => iM1j6mEB19ahvbtRpU41
                            [_score] => 1
                            [_source] => Array
                                (
                                    [Id] => 
                                    [First name] => 
                                    [Last name] => 
                                    [Email] => 
                                    [Phone] => 
                                    [Address] => 
                                    [Password] => 
                                    [Status] => 
                                    [createddate] => 
                                    [Updateddate] => 
                                )

                        )

                    [9] => Array
                        (
                            [_index] => employee
                            [_type] => employee_details
                            [_id] => cM1U6mEB19ahvbtRNk4F
                            [_score] => 1
                            [_source] => Array
                                (
                                    [Id] => 
                                    [First name] => 
                                    [Last name] => 
                                    [Email] => 
                                    [Phone] => 
                                    [Address] => 
                                    [Password] => 
                                    [Status] => 
                                    [createddate] => 
                                    [Updateddate] => 
                                )

                        )

                )

        )

)

任何人都可以帮我解析上面的数组,以html格式显示id,first_name,last_name,Email,密码,地址,状态,Createddate,更新日期的所有详细信息?

1 个答案:

答案 0 :(得分:3)

使用foreach从数组中获取每个元素,如下所示:

将上面的数组分配给一个数组变量(即这里我作为$result)。

foreach($result['hits']['hits'] as $each)
{
    echo $each['_source']['id'];
    echo $each['_source']['first_name'];
    echo $each['_source']['last_name'];
    echo $each['_source']['Email'];
    echo $each['_source']['Password'];
    echo $each['_source']['Address'];
    echo $each['_source']['Status'];
    echo $each['_source']['Createddate'];
    echo $each['_source']['Updateddate'];
}