【问题标题】:PHP filtering JSON arraysPHP过滤JSON数组
【发布时间】:2016-05-24 14:32:48
【问题描述】:

我是 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);

Feed 示例:

{ "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 为数组)。

有什么建议吗?

【问题讨论】:

  • 我没有看到“?”在这个“问题”中
  • 请显示您的.json 输入文件的示例。 将其添加到您的问题中,不要放在评论中
  • 至少他的代码有完整的文档记录......
  • foreach 循环中使用简单的if 语句来检查user 键的值——如果它不等于Max,则使用continues。

标签: php json parsing fullcalendar filtering


【解决方案1】:

感谢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();
    }}
}

【讨论】:

    猜你喜欢
    • 2020-10-30
    • 2013-03-07
    • 1970-01-01
    • 2018-05-04
    • 2018-11-01
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多