【问题标题】:i want to make custom json from multiple json object我想从多个 json 对象制作自定义 json
【发布时间】:2016-10-20 12:00:58
【问题描述】:

这是我的第一个 json

[
  {
    "future_sell": "2300.00",
    "to_currency": "DKK",
    "creation_datetime": "2016-10-20 12:23:29",
    "title": "Total selling currency",
    "title_two": "Date : 2016-10-20",
    "description": "DKK = 2300.00",
    "start": "2016-10-20",
    "end": "2016-10-20"
  },
  {
    "future_sell": "536.66",
    "to_currency": "USD",
    "creation_datetime": "2016-10-20 15:27:36",
    "title": "Total selling currency",
    "title_two": "Date : 2016-10-20",
    "description": "USD = 536.66",
    "start": "2016-10-20",
    "end": "2016-10-20"
  },
  {
    "future_sell": "600.00",
    "to_currency": "USD",
    "creation_datetime": "2016-10-21 13:51:28",
    "title": "Total selling currency",
    "title_two": "Date : 2016-10-21",
    "description": "USD = 600.00",
    "start": "2016-10-21",
    "end": "2016-10-21"
  }
]

我想从上面制作下面的json

[
  {

    "creation_datetime": "2016-10-20 12:23:29",
    "title": "Total selling currency",
    "title_two": "Date : 2016-10-20",
    "description": "DKK = 2300.00,USD = 536.66",
    "start": "2016-10-20",
    "end": "2016-10-20"
  },
  {
    "creation_datetime": "2016-10-21 13:51:28",
    "title": "Total selling currency",
    "title_two": "Date : 2016-10-21",
    "description": "USD = 600.00",
    "start": "2016-10-21",
    "end": "2016-10-21"
  }
]

表示如果 creation_datetime 字段中的相同日期和 Description 字段中的 to_currency 则要合并

我的数组在下面是第一个数组 json 对象

foreach ($future_sell_data as $key => $value) 
{
    $future_sell_data[$key]['title'] = "Total selling currency";
    $future_sell_data[$key]['title_two'] = "Date : " .date("Y-m-d",strtotime($value['creation_datetime']));
    $future_sell_data[$key]['description'] = $value['to_currency']." = ".$value['future_sell'];
    $future_sell_data[$key]['start'] = date("Y-m-d",strtotime($value['creation_datetime']));
    $future_sell_data[$key]['end'] = date("Y-m-d", strtotime($value['creation_datetime']));
}

谁能帮助我如何制作第二个 json 对象?

【问题讨论】:

  • 在您的示例第二个 json 中,creation_datetime 不一样......
  • 抱歉只是日期而不是时间
  • 你能帮帮我吗?
  • 为什么人们被否决了?我不明白?我的问题很真实,我想要解决方案。这就是我发布问题的原因....

标签: php arrays json codeigniter


【解决方案1】:
  1. 假设您不考虑时间
  2. 假设您有第一个要考虑的creation_datetime

foreach ($arr as $value) {
    $timestamp = $value["title_two"]; // Store the timestamp to combine similar values
    if(isset($customArr[$timestamp])) {
        $customArr[$timestamp]['description'] .= ",".$value['description'];
    } else {
        $customArr[$timestamp]['creation_datetime'] = $value['creation_datetime'];
        $customArr[$timestamp]['description'] = $value['description'];
    }
    $customArr[$timestamp]['title'] = $value['title'];
    $customArr[$timestamp]['title_two'] = $value['title_two'];
    $customArr[$timestamp]['start'] = $value['start'];
    $customArr[$timestamp]['end'] = $value['end'];
}

echo json_encode(array_values($customArr)); // timestamp is not needed for your JSON, so resetting the index

输出:Check here

[
  {
    "creation_datetime": "2016-10-20 12:23:29",
    "description": "DKK = 2300.00,USD = 536.66",
    "title": "Total selling currency",
    "title_two": "Date : 2016-10-20",
    "start": "2016-10-20",
    "end": "2016-10-20"
  },
  {
    "creation_datetime": "2016-10-21 13:51:28",
    "description": "USD = 600.00",
    "title": "Total selling currency",
    "title_two": "Date : 2016-10-21",
    "start": "2016-10-21",
    "end": "2016-10-21"
  }
]

【讨论】:

  • 你好,我没有任何字段,如标题,标题_二,描述,开始,结束我做了这个自定义,所以我尝试了你的解决方案,但它让我无效
  • 您好,您的解决方案正在运行,您可以删除每个项目中的 DATE 键吗??
  • 这就是为什么json_encode(array_values($customArr)) 声明的原因。阅读 cmets
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-04
  • 2020-05-16
  • 2018-09-18
  • 1970-01-01
  • 1970-01-01
  • 2016-09-05
  • 2019-05-28
相关资源
最近更新 更多