0

基本的に、配列内のオブジェクトの複雑な配列をソートしようとしています:

Array
(
    [190515] => stdClass Object
        (
            [nid] => 15740686
            [venue_nid] => 190515
            [occurrences] => 1
            [this_weeks_occurrences] => 0
            [end_date] => 1350853200
            [end_date_end_time] => 1350853200
            [is_ongoing] => 0
            [title] => Wentz Concert Hall and Fine Arts Center
            [times] => Array
                (
                    [0] => stdClass Object
                        (
                            [nid] => 15740686
                            [venue_nid] => 190515
                            [venue_title] => Wentz Concert Hall and Fine Arts Center
                            [datepart] => 20121021
                            [occurrences] => 1
                            [times] => Sun 4:00pm
                            [end_times] => Sun 4:00pm
                            [next_year] => 0
                            [next_month] => 0
                            [next_week] => 3
                            [occurrence_date] => 1350853200
                        )

                )

            [times_list] => Array
                (
                    [0] => Oct 21 sun 4:00pm
                )

        )

    [31403] => stdClass Object
        (
            [nid] => 15740686
            [venue_nid] => 31403
            [occurrences] => 1
            [this_weeks_occurrences] => 0
            [end_date] => 1350176400
            [end_date_end_time] => 1350176400
            [is_ongoing] => 0
            [title] => KAM Isaiah Israel
            [times] => Array
                (
                    [0] => stdClass Object
                        (
                            [nid] => 15740686
                            [venue_nid] => 31403
                            [venue_title] => KAM Isaiah Israel
                            [datepart] => 20121014
                            [occurrences] => 1
                            [times] => Sat 8:00pm
                            [end_times] => Sat 8:00pm
                            [next_year] => 0
                            [next_month] => 0
                            [next_week] => 2
                            [occurrence_date] => 1350176400
                        )

                )

            [times_list] => Array
                (
                    [0] => Oct 13 sat 8:00pm
                )

        )

    [33861] => stdClass Object
        (
            [nid] => 15740686
            [venue_nid] => 33861
            [occurrences] => 1
            [this_weeks_occurrences] => 0
            [end_date] => 1350781200
            [end_date_end_time] => 1350781200
            [is_ongoing] => 0
            [title] => Music Institute of Chicago, Nichols Concert Hall
            [times] => Array
                (
                    [0] => stdClass Object
                        (
                            [nid] => 15740686
                            [venue_nid] => 33861
                            [venue_title] => Music Institute of Chicago, Nichols Concert Hall
                            [datepart] => 20121021
                            [occurrences] => 1
                            [times] => Sat 8:00pm
                            [end_times] => Sat 8:00pm
                            [next_year] => 0
                            [next_month] => 0
                            [next_week] => 3
                            [occurrence_date] => 1350781200
                        )

                )

            [times_list] => Array
                (
                    [0] => Oct 20 sat 8:00pm
                )

        )

)

発生日でソートし、一番上のオブジェクト (例: 190515) のデータが他のオブジェクトで破損しないようにし、オブジェクトの「時間」[配列] に 2 番目の発生日が存在する可能性を含める必要があります。

ここでフィールドごとにオブジェクトの同様のソート配列を見てきましたが、配列/オブジェクトの深さではありません。usort を使用してみましたが、値に対する構文が正しくないと思います。

基本的に私が試したこと。

function cmp($x, $y) {
    if ($x->occurrence_date > $y->occurrence_date) {
      return 1; }
    else {
      return -1; }
 }
usort($node->timeout_events_schedule->venues, 'cmp');

前もって感謝します

4

2 に答える 2

3

どうですか:

function compare($x,$y)
{
    if($x->times[0]->occurrence_date == $y->times[0]->occurrence_date)
        return 0;
    elseif($x->times[0]->occurrence_date < $y->times[0]->occurrence_date)
        return -1;
    else
        return 1;

}

uasort($your_array,'compare');

usort() http://www.php.net/manual/en/function.uasort.phpとは異なり、uasort() はキーを保存します。

于 2012-10-05T20:39:01.307 に答える
2

問題の解決に役立ついくつかのヒント:

  • 連想キーを保持するには、uasort関数が必要です。
  • またはoccurrence_dateからキーに直接アクセスすることはできません。$x$y$x->times[0]->occurence_date
  • 比較を行う前に、$x->timesさらにエントリがあるかどうかを確認するために繰り返し、ニーズに合ったものを選択してください。
  • occurence_date文字列比較関数を使用する必要がない数値であるため

幸運を :)

于 2012-10-05T21:02:02.480 に答える