Amit Chauhan Amit Chauhan - 1 month ago 3
JSON Question

i want to make custom json from multiple json object

heres my first 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"
}
]


and I want to make below json from above

[
{

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


means want to combine if same date in creation_datetime field and its to_currency in Description field

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']));
}


can anyone help me how do i make second json object ?

Answer
  1. Assuming that you are not considering time
  2. Assuming that you have first creation_datetime to be considered

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

Output: 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"
  }
]
Comments