2016-02-13 3 views
-1

Я новичок в php, и я пытаюсь фильтровать JS FullCalendar JSON с дополнительным параметром, но пока не нашел решения. Мне удалось сделать индексный файл для отправки параметра в запрос, но не смог отфильтровать ближайшие данные с ним, так как проверка события - это диапазон дат в последних строках этого файла php.PHP-фильтрация массивов JSON

<?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 будет массив)

Любые предложения

+1

я не вижу «?» в этом «вопрос» –

+0

Показать пример вашего '.json' входного файла, пожалуйста. _ADD это на ваш вопрос, не положите его в comment_ – RiggsFolly

+0

по крайней мере, его код полностью задокументирован. –

ответ

0

Благодаря 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(); 
    }} 
} 
Смежные вопросы