我有2个JSON数组:
尽管每个JSON文件中的数据本身相同(房屋属性数据:浴室,卧室,描述,图像等),但$ json1_array使用不同的$键,并且与$ 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,我们可以看到:
但是,看看$ json2_array:
鉴于我想要达到的明显复杂性,我想知道它是否可能?当然,如果有可能如何?
定义问题:如何将多维json数组的$ keys和结构转换为与另一个json数组完全匹配,同时考虑可能需要合并的$ keys。
我希望看到$ json1_array与$ json2_array具有相同的结构。
由于
答案 0 :(得分:1)
您可以通过迭代要更改的数组来创建新数组。您可以检查要更改的密钥并指定新的密钥名称。
免责声明:我不是php开发人员,所以这是一个非常低效的方法。您应该查看array_map
函数,以使这更简单,更快。
<?php
// Dummy data
$rawArray1 = array('Name' => 'Test 1', 'PropType' => 'Test', 'Address 1' => 'Long address', 'Address 2' => 'Longer part 2');
// Structure to match
$rawArray2 = array('Name' => 'Test 2', 'Type' => '', 'Address' => '');
$jsonArray1 = json_encode($rawArray1);
$jsonArray2 = json_encode($rawArray2);
// End dummy data
// Data request api 1
$obj1 = json_decode($jsonArray1, true);
// Data request api 2
$obj2 = json_decode($jsonArray2, true);
$newArr = array();
// Loop through the array you want to change
// We will create a new array specifying the names of the new keys we want
foreach($obj1 as $key => $value) {
// Identify the key you want to change
if ($key == 'Address 1' || $key == 'Address 2') {
// Concatenate two keys
if (isset($newArr[ 'Address' ])) {
$newArr[ 'Address' ] = $newArr[ 'Address' ] . $value;
} else {
$newArr[ 'Address' ] = $value;
}
continue;
}
// Simply change the name of the key
if ($key == 'PropType') {
$newArr['Type'] = $value;
continue;
}
$newArr[ $key ] = $value;
}
var_dump($newArr);
// Encode to use
$data = json_encode($newArr);
?>