【问题标题】:PHP Script Results in Warnings of array_merge and usort. Resulting files are nullPHP 脚本导致 array_merge 和 usort 警告。结果文件为空
【发布时间】:2011-07-26 22:22:46
【问题描述】:

我对这件事很困惑。我的服务器上有一个 PHP 文件。它被编写用于从 Google 日历中提取事件信息并将结果数据写入单独的文本文件。 PHP 文件在 7 月 1 日之前一直运行良好。我不确定发生了什么导致错误,但是另一个人正在使用相同的脚本(他编写了它,并且对他来说仍然可以正常工作),所以我很确定这不是结果谷歌改变了一些东西。这是访问文件时返回的具体错误。

`警告:array_merge() [function.array-merge]:参数 #2 不是第 111 行 /home/westfork/public_html/AppData/DataSources/index.php 中的数组

警告:usort() [function.usort]:参数应该是第 120 行 /home/westfork/public_html/AppData/DataSources/index.php 中的数组

警告:array_merge() [function.array-merge]:参数 #2 不是第 111 行 /home/westfork/public_html/AppData/DataSources/index.php 中的数组

警告:usort() [function.usort]:参数应该是第 120 行 /home/westfork/public_html/AppData/DataSources/index.php 中的数组

警告:array_merge() [function.array-merge]:参数 #2 不是第 111 行 /home/westfork/public_html/AppData/DataSources/index.php 中的数组

警告:usort() [function.usort]:参数应该是第 120 行 /home/westfork/public_html/AppData/DataSources/index.php 中的数组

警告:array_merge() [function.array-merge]:参数 #2 不是第 111 行 /home/westfork/public_html/AppData/DataSources/index.php 中的数组

警告:usort() [function.usort]:参数应该是第 120 行 /home/westfork/public_html/AppData/DataSources/index.php 中的数组

警告:array_merge() [function.array-merge]:参数 #2 不是第 111 行 /home/westfork/public_html/AppData/DataSources/index.php 中的数组

警告:usort() [function.usort]:参数应该是第 120 行 /home/westfork/public_html/AppData/DataSources/index.php 中的数组

警告:array_merge() [function.array-merge]:参数 #2 不是第 111 行 /home/westfork/public_html/AppData/DataSources/index.php 中的数组

警告:usort() [function.usort]:参数应该是第 120 行 /home/westfork/public_html/AppData/DataSources/index.php 中的数组

警告:array_merge() [function.array-merge]:参数 #2 不是第 111 行 /home/westfork/public_html/AppData/DataSources/index.php 中的数组

警告:usort() [function.usort]:参数应该是第 120 行 /home/westfork/public_html/AppData/DataSources/index.php 中的数组

警告:array_merge() [function.array-merge]:参数 #2 不是第 111 行 /home/westfork/public_html/AppData/DataSources/index.php 中的数组

警告:usort() [function.usort]:参数应该是 /home/westfork/public_html/AppData/DataSources/index.php 第 120 行中的数组`

这是 PHP 文件。任何帮助都会很棒。

<?php

$today = date("F j, Y");
$thisWeek = date("W");
$thisYear = date("Y");


/*
 *  Represents individual events within a calendar
 */
Class Event
{   
public $allDay; // "1" or ""
public $multiDay; // "1" or ""
public $date; // "F j, Y"
public $time; // String
public $start; // Timestamp
public $end; // Timestamp
public $section; // String
public $title; // String
public $description; // String
public $location; // String

/**
 * @param array $event - an entry node from the Google calendar feed
 */
public function __construct($event) 
{               
    // set the Event properties based on the array keys and values
    foreach($event as $key=>$value) {
        $this->{$key} = $value;
    }

    $this->time = $this->relativeTime();

    // set the section header for this event
    $this->section = $this->sectionHeader();

}

/**
 * @return string - the section heading based on when the event is in relation to the 
 * current date, e.g., "Today", "This Week", "May", "Dec", etc.
 */
protected function sectionHeader() {
    global $today, $thisWeek, $thisYear;

    // determine which section header to apply
    if (strtotime($today) == $this->date) {
        return "Today";
    } else if (date("F j, Y", strtotime($today) + (60*60*24)) === date("F j, Y", $this->date)) {
        return "Tomorrow";
    } else if ($thisYear === date("Y", $this->date)) { 
        return date("l, F j", $this->date);
    } else {
        return date("l, F j, Y", $this->date);
    }
}

protected function relativeTime() {
    // if the event isn't and all-day event, set the relative start and end times
    if (!$this->allDay) {

        // if the start and end times are outside of the current day, set $allDay to "1"
        if ($this->start <= $this->date && $this->end >= $this->date + (60*60*24)) {
            return "All Day";
        }
        // if both the start and end are within the current day
        else if ($this->start > $this->date && $this->end < $this->date + (60*60*24)) {
            return date("g:i A", $this->start) . " - " . date("g:i A", $this->end);
        }

        // if the start is within the current day
        else if ($this->start > $this->date && $this->start < $this->date + (60*60*24)) {
            return date("g:i A", $this->start);
        }
        // if the end is within the current day
        else if ($this->end > $this->date && $this->end < $this->date + (60*60*24)) {
            return 'ends: ' . date("g:i A", $this->start);
        }
    } else {
        return "All Day";
    }
}


}

/*
 *  Represents a calendar containing events
 */
Class Calendar
{
protected $events = array();
protected $name;

/**
 * @param String $name - the name of the calendar (will be used as the file name for saving)
 */
public function __construct($name) 
{
    $this->name = $name;
}

/**
 * Adds a an array of Event to the calendar. 
 *  @param array $events - an array of Events
 */
public function addEvents($events)
{
    $this->events = array_merge($this->events, $events);
    $this->sortEvents();
}

/**
 * Sorts the $this->events array 
 */
protected function sortEvents()
{
    usort($this->events, array($this, 'compareTwoEventsForSort'));
}

/**
 * Compare function for sortEvents()
 * Compares first by Event::$date and then Event::$start
 */
protected function compareTwoEventsForSort($x, $y) {

    // compare the dates
    if ( $x->date < $y->date) {
      return -1;
    }
    if ( $x->date > $y->date) {
        return 1;
    }

    // the dates are equal, so now compare the times
    if ($x->start < $y->start) {
        return -1;
    } else if ($x->start > $y->start) {
        return 1;
    } else {
        return 0;
    }

}

public function listEvents() {
    print_r($this->events);
}

public function saveCalendarAsFile() {
    // Log the results into separate calendar text files.
    $file = fopen("{$this->name}.txt", 'w');
    fwrite($file, json_encode($this->events));
    fclose($file);      
}   
}

/*
 *  Handles the creation and saving of Calendars and Events
 */
Class CalendarController
{

public $calendar;

/**
 * @param String $id - the id of the google calendar
 */
public function __construct($name, $id)
{       
    $today = date("Y-m-d");

    if ( strtotime(date("Y") . "-06-01") > time()) {
        $yearEnd = date("Y") . "-08-01";
    } else {
        $yearEnd = ((int) date("Y") + 1) . "-08-01";
    }

    $url = "https://www.google.com/calendar/feeds/{$id}/public/";
    $url .= "full?";
    $url .= "&start-min=$today&start-max=$yearEnd";
    $url .= "&max-results=1000";
    $url .= "&orderby=starttime";
    $url .= "&sortorder=a";
    $url .= "&singleevents=true";
    $url .= "&ctz=America/Chicago";
    $url .= "&fields=entry(title,content,gd:where,gd:when)";

    $xml = file_get_contents($url);

    // get rid of the namespace "gd:"
    $xml = str_replace("gd:", "", $xml);
    $xml = new SimpleXMLElement($xml);

    $events = $this->parseEventsFromGoogleXML($xml);
    $this->calendar = new Calendar($name);
    $this->calendar->addEvents($events);
}

public function listCalendarEvents() {
    $this->calendar->listEvents();
}

public function saveCalendarAsFile() {
    $this->calendar->saveCalendarAsFile();
}

/**
 * @param SimpleXMLElement $xml - a simpleXML object of the Google calendar feed
 */
protected function parseEventsFromGoogleXML($xml)
{
    global $today;

    $events = array();
    foreach ($xml->entry as $entry) {

        $start = strtotime($entry->when['startTime']);
        $end = strtotime($entry->when['endTime']);

        // if the $end time is 12:00 AM the next morning, make it 11:59 PM the previous day
        if (date("H:i", $end) === "00:00") {
            $end -= 1;
        }

        $startDate = date("F j, Y", $start);
        $endDate = date("F j, Y", $end);

        $allDay = (strpos($entry->when['startTime'] . $entry->when['endTime'], "T") === false) ? "1" : "";
        $multiDay = ($startDate !== $endDate) ? "1" : "";

        // iterate through each day of the event
        $loopEndDate = strtotime($endDate);
        $loopToday = strtotime($today);
        $loopDate = strtotime($startDate);

        for ( ; $loopDate <= $loopEndDate; $loopDate += (60*60*24) )
        {               
            if ($loopDate >= $loopToday) {

                // clean up the description text
                // replace <br> tags with \n, delete \t characters, and remove all non-link tags
                $description = (string) $entry->content;
                $description = trim($description);
                $description = preg_replace("/<br[^>]*>/i", "\n", $description);
                $description = preg_replace("/\n */", "\n", $description);
                $description = preg_replace("/\n{3,}/", "\n\n", $description);
                $description = strip_tags($description, "<a>");
                $description = preg_replace("/([ \t]*$)|(^[ \t]*)/m", "", $description);
                $description = htmlspecialchars($description, ENT_NOQUOTES, 'UTF-8', false);

                $event = new Event(array(
                    'date' => $loopDate,
                    'allDay' => $allDay,
                    'multiDay' => $multiDay,
                    'start' => $start,
                    'end' => $end,
                    'title' => (string) $entry->title,
                    'description' => $description,
                    'location' => (string) $entry->where['valueString'],
                ));
                $events[] = $event;
            }
        }
    }
    return $events;
}

}


$calendars = array(
"Academic" => "westmont.edu_tker7t4jgfoi6i7msien0smimk@group.calendar.google.com",
"Athletics" => "westmont.edu_a2o3k05co4i3tt5voc1ginging@group.calendar.google.com",
"Chapel" => "westmont.edu_olec4f6vb91cn02e9higcab57c@group.calendar.google.com",
"Events" => "westmont.edu_mnjvcd9e9224q9m723nurbl8ks@group.calendar.google.com",
"Dining" => "3mipcafj6qeqs1m9mfpngv97q0@group.calendar.google.com",
"Band" => "westforkband.org_kg9n1kin34c3eovt6l2gomvlb8%40group.calendar.google.com",
"Rehearsal" => "westforkband.org_mvibrgiu71ldvrnfh6r4rdf3fc@group.calendar.google.com",
"Parking" => "westforkband.org_t8jj4b5l9b3li2n31ce67ecc84@group.calendar.google.com",
// "Development" => "westmont.edu_atsog3h7vsf4k3s5r1f6e2p660%40group.calendar.google.com",

);


foreach($calendars as $name=>$id) {
$ecc = new CalendarController($name, $id);
$ecc->listCalendarEvents();
$ecc->saveCalendarAsFile();
}

【问题讨论】:

  • 抱歉,无法重现。至少不在灯 5.3.5 上
  • 请将您的代码精简到最基本的部分。那些渴望得分的人可能会付出努力,那些不太愿意的人可能会投票结束你的问题。我们真的很喜欢简单的输入 -> 最少的代码 -> 输出。这样做,您甚至可能自己发现错误。
  • 大家好。对此很抱歉,但无法重现这些问题的原因是因为它们显然不存在于我给你的代码中。出于某种原因,我对文件所做的任何更改(包括删除)都会反映在基于 Web 的文件管理器中,但实际上并未应用。我必须使用 FTP 来进行任何永久性文件更改。我不确定这是怎么回事,但问题已经消失了。非常感谢大家给予的帮助。很抱歉占用您的时间。

标签: php


【解决方案1】:

您是否尝试过在addEvents 方法中输出$events 参数中的内容?

尝试在第 111 行之前执行var_dump($events);,看看会发生什么。.

var_dump($events);
$this->events = array_merge($this->events, $events);

这将显示$events 的完整结构,包括属性类型。如果这是通过 cron 作业或其他东西运行的,您可能希望将 var 输出到日志文件或其他东西。

此外,您最好在尝试合并之前检查 $events 是否是一个数组:

if (is_array($events)) {
    $this->events = array_merge($this->events, $events);
    $this->sortEvents();
}

如果您每次都应该是一个数组,您仍应保留该条件,但添加一个 else,通过日志通知您 $events 是什么,以便您可以在这些情况下正确处理它。

【讨论】:

    【解决方案2】:

    不想这么说,但我粘贴了提供的代码,它对我有用(感谢给定的日历地址)。 parseEventsFromGoogleXML 必须失败,可能是因为连接错误,或者时间限制,或者其他任何原因。你需要处理它。在$events = $this-&gt;parseEventsFromGoogleXML($xml); 之前将$events 设为空数组,使用异常并尝试/捕获它们,在继续之前检查连接和/或XML 结果等。确保您的服务器可以从外部处理文件。

    【讨论】:

      猜你喜欢
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-30
      • 1970-01-01
      • 2014-01-31
      相关资源
      最近更新 更多