首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP JSON API -不必在Eveytime中定义位置

PHP JSON API -不必在Eveytime中定义位置
EN

Stack Overflow用户
提问于 2018-06-02 02:25:09
回答 2查看 35关注 0票数 0

你好,我用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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-02 02:59:48

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

<?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']);
票数 0
EN

Stack Overflow用户

发布于 2018-06-02 02:58:08

您现在正在做的是:

[..]
'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或更高版本时,您可以使用(新的) coalesce operator来缩短这段时间

[..]
'store' => array(
    'name' => $store['included'][0]['attributes']['name'] ?? '',
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50649138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档