显示json数据条件

时间:2016-03-17 17:09:38

标签: php arrays json

我有一个数据,我想只显示1个数据,从json数据不知道,只显示1个数据。我想将这些数据复制到我的页面。

这是我的代码 的 json.php

  <?php 
    $response = curl_exec($curl);
            $err = curl_error($curl);
            curl_close($curl);

            if ($err) {
              echo "cURL Error #:" . $err;
            } else {
                $array = json_decode($response, true);
                echo '<pre>'; 
                print_r($array);
            } ?>

这是我的数据

 Array
(
    [rajaongkir] => Array
        (
            [query] => Array
                (
                    [origin] => 501
                    [destination] => 114
                    [weight] => 1700
                    [courier] => jne
                    [service] => REG
                )

            [status] => Array
                (
                    [code] => 200
                    [description] => OK
                )

            [origin_details] => Array
                (
                    [city_id] => 501
                    [province_id] => 5
                    [province] => DI Yogyakarta
                    [type] => Kota
                    [city_name] => Yogyakarta
                    [postal_code] => 55222
                )

            [destination_details] => Array
                (
                    [city_id] => 114
                    [province_id] => 1
                    [province] => Bali
                    [type] => Kota
                    [city_name] => Denpasar
                    [postal_code] => 80227
                )

            [results] => Array
                (
                    [0] => Array
                        (
                            [code] => jne
                            [name] => Jalur Nugraha Ekakurir (JNE)
                            [costs] => Array
                                (
                                    [0] => Array
                                        (
                                            [service] => OKE
                                            [description] => Ongkos Kirim Ekonomis
                                            [cost] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [value] => 42000
                                                            [etd] => 4-5
                                                            [note] => 
                                                        )

                                                )

                                        )

                                    [1] => Array
                                        (
                                            [service] => REG
                                            [description] => Layanan Reguler
                                            [cost] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [value] => 48000
                                                            [etd] => 2-3
                                                            [note] => 
                                                        )

                                                )

                                        )

                                    [2] => Array
                                        (
                                            [service] => YES
                                            [description] => Yakin Esok Sampai
                                            [cost] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [value] => 104000
                                                            [etd] => 1-1
                                                            [note] => 
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

我想只显示1个数据,我想显示此数据

[1] => Array
                                        (
                                            [service] => REG
                                            [description] => Layanan Reguler
                                            [cost] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [value] => 48000
                                                            [etd] => 2-3
                                                            [note] => 
                                                        )

                                                )

                                        )

帮助我谢谢

1 个答案:

答案 0 :(得分:1)

如果您只是在多维数组中寻找该数组。你会做以下。

$array['rajaongkir']['results'][0]['costs'][1]