【问题标题】:How to parse the array in codeigniter?如何解析codeigniter中的数组?
【发布时间】:2018-03-08 12:16:51
【问题描述】:

我有下面的数组结构如何解析这个数组并在 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、Password、Address、Status、Createddate、Updateddate 的所有详细信息?

【问题讨论】:

标签: codeigniter


【解决方案1】:

使用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'];
}  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多