使用 PHP 重新映射 $json1 键和结构以适应 $json2 键和结构

Remap $json1 keys and structure to fit $json2 keys and structure using PHP

我有 2 个 JSON 数组:

尽管数据本身在每个 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 我们可以看到:

但是,查看 $json2_array:

考虑到我想要实现的目标非常复杂,我想知道这是否可能?当然,如果可能的话,怎么做?

定义问题: 如何将多维 json 数组的 $keys 和结构转换为与另一个 json 数组的完全匹配,同时考虑 $可能需要合并的键。

我希望看到 $json1_array 具有与 $json2_array.

相同的结构

谢谢

您可以通过遍历要更改的数组来创建新数组。您可以检查要更改的密钥并指定新的密钥名称。

免责声明:我不是 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);

?>