【发布时间】:2017-07-15 09:07:27
【问题描述】:
我有 2 个 JSON 数组:
- $json1_array : 来自第 3 方 #1
- $json2_array : 来自第 3 方 #2
尽管每个 JSON 文件中的数据本身都是相同的(房屋属性数据:浴室、卧室、描述、图像等)$json1_array 使用不同的 $keys,并且与 $json2_array 具有不同的结构。
我发现很多问题,人们试图将结构更改为单个 JSON 数组并更改其键,但这一直是使用单个 JSON 数组。 但是我希望将 $json1_array 转换为匹配 $json_array2 的结构。
我已经查看了返回了不需要的结果的 array_merge,例如将 $json1 $keys 放入我不想要的 $json2_array 数组中。
我尝试了一种方法,使用以下方法重写 $json1 中的键以适应 $json2_array:
public function rewriteKeys($newKeyFormat, $newArr)
{
$newKeyFormat = array(
'address1' => 'address',
'address2' => 'address',
'city' => 'city',
'state' => 'state',
'zip_code' => 'postal_code',
}
[Note: Could not get this to work]
当我尝试将转换地址 1、地址 2 重写为只是地址时,会发生挣扎。我猜想在这部分工作中我需要array_merge 函数。
$json1_array(由于长度的片段):
{"data":[{
"id":32,
"last_update":"2016-08-31 15:06:13",
"address":{
"address1":"Villa Chicca",
"address2":null,
"city":"LEZZENO",
"state":"Province Of Como",
"zip_code":"-",
"country":"Italy",
"details":{
"dwelling_name":"Villa Chicha",
"dwelling_type":"Villa\/Cottage",
"maximum_capacity":8,
"base_capacity":8,
"bedrooms":4,
"bathrooms":3,
"currency":"EUR"},
"urls":[{
"type":"Main",
"url":"www.google.com"},{
"type":"Contact owner",
"url":"www.google.com#contact-owner"},{
"type":"Contact",
"url":"www.google.com#contact-us"}],
"descriptions":{
"dwelling_description":"This besuch as catering, cooking lessons and spa treatments.",
"rate_description":null,
"location_description":"The area aroAirport: 100 km",
"capacity_info":null,
"catering_services":"Servit can really make the difference.",
"wedding_conference_info":null,
"special_offers":null,
"interior_grounds":"rThe Vable if required",
"getting_there":null,
"terms_and_conditions":"\r\n"},
"extras":["Maid servic","Chef\/Cook available",],
"amenities":["Wi-Fi"],
"photos":[
{
"order":0,
"url":"www.google.com_1_.jpg?utm_source=feeds&utm_medium=feeds&utm_campaign=melt"
}
,{
"order":1,
"url":"www.google.com?utm_source=feeds&utm_medium=feeds&utm_campaign=melt"
},{
"order":2,
"url":"hwww.google.com?utm_source=feeds&utm_medium=feeds&utm_campaign=melt"},
{
$json2_array (片段由于长度):
"id": 0,
"host_id": 0,
"cleaning_time": 0,
"cleaning_time_min": 0,
"cleaning_fee": true,
"title": "string",
"size": 0,
"checkin": "string",
"checkout": "string",
"created_at": "string",
"updated_at": "string",
"deleted_at": "string",
"deactivated_at": "string",
"availability_updated_at": "string",
"rates_updated_at": "string",
"location":
"id": 0,
"country_iso2": "string",
"latitude": "string",
"longitude": "string",
"city": "string",
"country_name": "string",
"state_province": "string",
"postal_code": "string",
"address": "string",
"timezone": "string"
,
"rate":
"version": 0,
"daily_default": 0,
"weekly_percentage_decrease": 0,
"monthly_percentage_decrease": 0,
"weekend_increase": 0,
"minimum_stay": 0,
"maximum_stay": 0,
"extra_person_fee": 0,
"extra_person_fee_trigger_amount": 0
,
"property_id": ,
"portal_title":
"attribute_name": "string",
"translations":
"id": 0,
"subject": "string",
"content": "string",
"language_id": 0,
"container_id": 0,
"container_type": "Listing",
"attribute_name": "string"
],
"id": 0,
"subject": "string",
"content": "string",
"language_id": 0,
"container_id": 0,
"container_type": "Listing"
,
"description":
"attribute_name": "string",
"translations":
"id": 0,
"subject": "string",
"content": "string",
"language_id": 0,
"container_id": 0,
"container_type": "Listing",
"attribute_name": "string"
],
"id": 0,
"subject": "string",
"content": "string",
"language_id": 0,
"container_id": 0,
"container_type": "Listing"
,
查看 $json1_array 我们可以看到:
- 地址->地址1
- 地址->地址2
- 地址 -> 城市
不过,看看 $json2_array:
- 位置 -> 地址
- 位置 -> 城市
鉴于我要实现的目标的明显复杂性,我想知道它是否可能?当然,如果可能的话怎么做?
已定义的问题:考虑到可能需要合并的 $keys,如何将多维 json 数组的 $keys 和结构转换为与另一个 json 数组的结构完全匹配。
我希望看到 $json1_array 与 $json2_array 具有相同的结构。
谢谢
【问题讨论】:
-
作为一个社区网站,你会认为如果用户希望标记某些内容,他们至少应该提供推理,因为它并不能完全帮助我通过看到负面标记来改善我的问题推理。
-
不是我的反对意见,但这可能是因为这基本上是要求某人为您编写代码,而这不是 SO 的用途。我们最愿意帮助您修复一些未达到您希望的代码,但我们不会全部为您编写。
-
我会留下这些,以防万一;) php.net/manual/en/function.array-combine.php 和 php.net/manual/en/function.array-merge.php .. 哦,顺便说一句,你试过使用 stdClass 吗?真的很简单!
-
为 Riggs 的解释干杯——至少这更有用一点。也就是说,我从 9 月开始就一直在做这个项目,我不是服务器端开发人员,当然也不是 API 开发人员,所以我迄今为止所制作的内容是一种边走边学的方法,我也很高兴鉴于我的非编程背景,我制作的内容。
标签: php json multidimensional-array converter