PHP 过滤 JSON 数组



我是 php 的新手,我正在尝试使用额外的参数过滤 JS FullCalendar JSON 提要,但尚未找到解决方案。我设法制作索引文件以将参数发送到查询,但无法使用它过滤来的数据,例如检查事件是此 php 文件最后一行的日期范围。

   <?php
    //--------------------------------------------------------------------------------------------------
    // This script reads event data from a JSON file and outputs those events which are within the range
    // supplied by the "start" and "end" GET parameters.
    //
    // An optional "timezone" GET parameter will force all ISO8601 date stings to a given timezone.
    //
    // Requires PHP 5.2.0 or higher.
    //--------------------------------------------------------------------------------------------------
    // Require our Event class and datetime utilities
    require dirname(__FILE__) . '/utils.php';
    // Short-circuit if the client did not give us a date range.
    if (!isset($_GET['start']) || !isset($_GET['end'])) {
        die("Please provide a date range.");
    }
    // Parse the start/end parameters.
    // These are assumed to be ISO8601 strings with no time nor timezone, like "2013-12-29".
    // Since no timezone will be present, they will parsed as UTC.
    $range_start = parseDateTime($_GET['start']);
    $range_end = parseDateTime($_GET['end']);
   // Get user info 
    $user = $_GET['user'];
    // Parse the timezone parameter if it is present.
    $timezone = null;
    if (isset($_GET['timezone'])) {
        $timezone = new DateTimeZone($_GET['timezone']);
    }
    // Read and parse our events JSON file into an array of event data arrays.
    $json = file_get_contents(dirname(__FILE__) . '/../json/events.json');
    $input_arrays = json_decode($json, true);

    // Accumulate an output array of event data arrays.
    $output_arrays = array();
    foreach ($input_arrays as $array) {
        // Convert the input array into a useful Event object
        $event = new Event($array, $timezone);
        // If the event is in-bounds, add it to the output
        if ($event->isWithinDayRange($range_start, $range_end)){
            $output_arrays[] = $event->toArray();
        }
    }
    // Send JSON to the client.
    echo json_encode($output_arrays);

提要示例:

{ "user": "Max", "company": "ex1",     "start": "2016-03-16T07:00:00",     "end": "2016-03-16T14:30:00",     "info": " "},
{ "user": "Max", "company": "ex2",      "start": "2016-03-17T07:00:00",     "end": "2016-03-17T14:30:00",     "info": " "},
{ "user": "Sam", "company": "e3",      "start": "2016-03-18T07:00:00",     "end": "2016-03-18T14:30:00",     "info": " "},

目的是仅获取用户"Max"的行。我尝试了各种PHP函数,但它总是给出以下错误: (警告消息"警告:in_array() 预期参数 2 为数组)。

有什么建议吗?

感谢CBroe,我得到了它。

// Accumulate an output array of event data arrays.
$output_arrays = array();
foreach ($input_arrays as $array) {
  if($array['user'] == $user) {
    // Convert the input array into a useful Event object
    $event = new Event($array, $timezone);
    // If the event is in-bounds, add it to the output
    if ($event->isWithinDayRange($range_start, $range_end)){
        $output_arrays[] = $event->toArray();
    }}
}

相关内容

  • 没有找到相关文章

最新更新