【问题标题】:Remap $json1 keys and structure to fit $json2 keys and structure using PHP使用 PHP 重新映射 $json1 键和结构以适应 $json2 键和结构
【发布时间】: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.phpphp.net/manual/en/function.array-merge.php .. 哦,顺便说一句,你试过使用 stdClass 吗?真的很简单!
  • 为 Riggs 的解释干杯——至少这更有用一点。也就是说,我从 9 月开始就一直在做这个项目,我不是服务器端开发人员,当然也不是 API 开发人员,所以我迄今为止所制作的内容是一种边走边学的方法,我也很高兴鉴于我的非编程背景,我制作的内容。

标签: php json multidimensional-array converter


【解决方案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);

?>

【讨论】:

  • 为我的帮助干杯乔希,我明白你在这里所做的事情,所以我想稍微编辑一下,我应该可以使用它。肖恩(法医肖恩)告诉我,我应该从头开始构建它并使用 Java / JS / AngularJS,所以我认为必须是 2.0 xD 再次感谢您的输入伙伴。
  • 注意:对于阅读 cmets 的其他人,我将此回复设置为答案,因为我的问题是询问是否有办法实现我的目标以及我应该使用什么方法。一旦我找到构建我的解决方案,我将更新我的初始帖子,以便其他有类似目标的人可能会从这个问题中受益。
猜你喜欢
  • 1970-01-01
  • 2020-11-08
  • 1970-01-01
  • 2019-01-18
  • 2014-10-03
  • 2021-12-05
  • 2023-03-30
  • 2011-11-02
  • 1970-01-01
相关资源
最近更新 更多