我想从多个json对象中创建自定义json


i want to make custom json from multiple json object

这是我的第一个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

my array is below for first array json object

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对象?

  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"
  }
]