如何获取数组记录?

时间:2015-05-01 12:23:40

标签: php arrays json

我使用

从API获取数组
<?php
$json = json_decode(file_get_contents("API_URL"), TRUE);
echo '<pre>';
print_r($json);
?>

我得到了那样的结果

    Array
    (
        [deals] => Array
            (
                [0] => Array
                    (
                        [activeDa

te] => 1430332361000
                    [bogo] => 
                    [categories] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 29057
                                    [name] => Bath & Body
                                )

                            [1] => Array
                                (
                                    [id] => 20733
                                    [name] => Cosmetics
                                )

                            [2] => Array
                                (
                                    [id] => 29190
                                    [name] => Skin Care
                                )

                            [3] => Array
                                (
                                    [id] => 20856
                                    [name] => Fragrances
                                )

                            [4] => Array
                                (
                                    [id] => 29059
                                    [name] => Beauty & Personal Care
                                )

                        )

                    [clearance] => 
                    [couponCode] => HAPPYMOM
                    [dealImageUrl] => http://cdn.savings.com/logo/1737578.png
                    [dealUrl] => http://www.savings.com/m/p/19561077/8306099/c?afsrc=1&up=2015-05-01-05-15
                    [description] => Go through this link to get Assorted Spring Getaway tote for only $20 on orders $40 or more, save 80%. Restrictions may apply. Limited time offer only or when supplies run out.
                    [discount] => 1
                    [exclusive] => 
                    [freeShipping] => 
                    [homePageStaffPick] => 
                    [id] => 3862713
                    [lastUpdated] => 1430332361000
                    [merchantDisplayUrl] => http://www.bathandbodyworks.com
                    [merchantId] => 236514
                    [merchantImageUrl] => http://cdn.savings.com/logo/1737578.png
                    [merchantName] => Bath and Body Works
                    [merchantPageStaffPick] => 
                    [merchantScore] => 17
                    [merchantUrl] => http://www.savings.com/m/p/19561077/1742990/c?afsrc=1
                    [minimumSpend] => 0.00
                    [mobileMonetized] => 
                    [monetized] => 1
                    [printable] => 
                    [promotion] => 80% Off
                    [rebate] => 
                    [scope] => SITE_WIDE
                    [score] => 579
                    [siteUrls] => Array
                        (
                            [0] => http://www.bathandbodyworks.com
                        )

                    [startDate] => 1430290800000
                    [tip] => 
                    [title] => Get 80% off Assorted Spring Getaway Tote on Orders Over $40 - Only $20
                    [validated] => 
                    [voteDown] => 0
                    [voteUp] => 0
                )

我的问题是我如何获得[deals][categories][0][name]的价值? 我想存储类别名称的值。

2 个答案:

答案 0 :(得分:1)

你已经有了答案。您需要做的唯一事情是使它们成为文字字符串并获取deals数组中的第一个元素。所以:

echo $json['deals'][0]['categories'][0]['name']

答案 1 :(得分:1)

你自己的问题中有答案。只需要一点修改。请试试这个: -

echo $yourarrayname['deals'][0]['categories'][0]['name'];

注意: - 因为类别是第零个交易索引。所以在类别索引之前放零指数。