Tim Herbert Tim Herbert - 1 year ago 70
JSON Question

My json file says ArrayArrayArray after sorting and storing a new array with php

So I need help with what should be a simple thing. I am retrieving this giant json file that has years worth of events. Sorting through them to get just last years and this years events. Storing them as a new array and trying to store them as another .json file so that I can use that with a calendar script I've written in Javascript.

My problem is that the resulting .json file just says ArrayArrayArrayArray for all the information instead of all the multidimensional arrays it should have for the information.

$string = file_get_contents('events.json');
$json = json_decode($string, true);
$new_array = array();

// variables to set the time available for calendar
$year = date("Y-m-d");
$startTime = date("Y-m-d", strtotime($year . '-1 year'));
$startTime = date('Y-m', strtotime($startTime));
$endTime = date('Y-m-d', strtotime($year . '+1 year'));
$endTime = date('Y-m', strtotime($endTime));
foreach($json as $key => $value){
$eventTime = $value['StartTime'];
$eventTime = date('Y-m', strtotime($eventTime));

if($eventTime >= $startTime && $eventTime <= $endTime){
array_push($new_array, $value);
//array_push($new_array, array($key => $value));

// Store contents of array for Calendars use.

file_put_contents('events1.json', $new_array);

Answer Source

The json_encode function doesn't modify its argument. It returns a new value. Try either

file_put_contents('events1.json', json_encode($new_array));


$encoded_array = json_encode($new_array);

file_put_contents('events1.json', $encoded_array);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download