你好,我用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
https://stackoverflow.com/questions/50649138
复制相似问题