2016-02-13 88 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": " "}, 

目的是隻能獲得用戶 「最大」 線。我曾嘗試過各種PHP功能,但它總是給錯誤,如: (警告信息:「警告:in_array()預計參數2爲array)

任何建議

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