我想从多个json对象

时间:2016-10-20 12:00:58

标签: php arrays json codeigniter

继承人我的第一个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字段中将相同日期与描述字段中的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对象?

1 个答案:

答案 0 :(得分:1)

  1. 假设你没有考虑时间
  2. 假设您有第一个creation_datetime被视为
  3. 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"
      }
    ]