PHP JSON API - 不必定义位置Eveytime?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (70)

我使用PHP制作了一个JSON API。唯一的问题是,我需要每次定义位置。因为它是一个相当大的API,这几乎是不可能的。

API是这样创建的:

            $api = array(
                array(
                    'store' => array(
                        array('name' => isset($store['included'][0]['attributes']['name']) ? $store['included'][0]['attributes']['name'] : ''),
                        array('skuid' => isset($store['included'][0]['attributes']['default-sku-id']) ? $store['included'][0]['attributes']['default-sku-id'] : ''),
                        array('releasedate' => isset($store['included'][0]['attributes']['release-date']) ? $store['included'][0]['attributes']['release-date'] : ''),
                        array('nonplusprice' => isset($store['included'][0]['attributes']['skus'][0]['prices']['non-plus-user']['actual-price']['display']) ? $store['included'][0]['attributes']['skus'][0]['prices']['non-plus-user']['actual-price']['display'] : ''),
                        array('psplusprice' => isset($store['included'][0]['attributes']['skus'][0]['prices']['plus-user']['actual-price']['display']) ? $store['included'][0]['attributes']['skus'][0]['prices']['plus-user']['actual-price']['display'] : ''),

                        array(
                            'filesize',
                            array('value' => isset($store['included'][0]['attributes']['file-size']['value']) ? $store['included'][0]['attributes']['file-size']['value'] : ''),
                            array('unit' => isset($store['included'][0]['attributes']['file-size']['unit']) ? $store['included'][0]['attributes']['file-size']['unit'] : ''),
                        ),

                        //...
                    )
                )

            return(json_encode($api));

现在,当我想使用这些值时,我需要这样做:

echo $json[0]['store'][1]['skuid']; // sku id
echo $json[0]['store'][4]['psplusprice']; // price

如何更改API,因此我不必指定位置(例如,对于本示例中的1和4)?

预期结果:

echo $json[0]['store']['skuid']; // sku id
echo $json[0]['store']['psplusprice']; // price
提问于
用户回答回答于

只要尝试删除不必要的级别,如下例所示:

<?php

            $api =[
                    ['store' => [
                        'name' => isset($store['included'][0]['attributes']['name']) ? $store['included'][0]['attributes']['name'] : '',
                        'skuid' => isset($store['included'][0]['attributes']['default-sku-id']) ? $store['included'][0]['attributes']['default-sku-id'] : '',
                        'releasedate' => isset($store['included'][0]['attributes']['release-date']) ? $store['included'][0]['attributes']['release-date'] : '',
                        'nonplusprice' => isset($store['included'][0]['attributes']['skus'][0]['prices']['non-plus-user']['actual-price']['display']) ? $store['included'][0]['attributes']['skus'][0]['prices']['non-plus-user']['actual-price']['display'] : '',
                        'psplusprice' => isset($store['included'][0]['attributes']['skus'][0]['prices']['plus-user']['actual-price']['display']) ? $store['included'][0]['attributes']['skus'][0]['prices']['plus-user']['actual-price']['display'] : '',
                        'filesize' => [
                            'value' => isset($store['included'][0]['attributes']['file-size']['value']) ? $store['included'][0]['attributes']['file-size']['value'] : '',
                            'unit' => isset($store['included'][0]['attributes']['file-size']['unit']) ? $store['included'][0]['attributes']['file-size']['unit'] : '',
                            ],
                        ],
                    ]
                ];

    var_dump($api[0]['store']['skuid']);
用户回答回答于

你现在要做的是:

[..]
'store' => array(
    array('name' => 'Some name'),
//  ^ Why did you type this array here?

你想做的是

[..]
'store' => array(
    'name' => 'Some name',

因此

[..]
'store' => array(
    'name' => isset($store['included'][0]['attributes']['name']) ? $store['included'][0]['attributes']['name'] : '',

使用PHP7.0或更高版本时,可以使用聚结算子:

[..]
'store' => array(
    'name' => $store['included'][0]['attributes']['name'] ?? '',

扫码关注云+社区

领取腾讯云代金券