你好,我用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
发布于 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']);
发布于 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'] ?? '',
https://stackoverflow.com/questions/50649138
复制相似问题