Uasort with array给出未定义的索引

时间:2014-03-12 04:02:01

标签: php multidimensional-array usort

我有以下数组作为示例

array(3) {
["date"]=>
    array(6) {
    [0]=>
    string(19) "2014-03-12 00:00:00"
    [1]=>
    string(19) "2014-03-12 08:30:00"
    [2]=>
    string(19) "2014-03-10 08:00:00"
    [3]=>
    string(19) "2014-03-11 08:00:00"
    [4]=>
    string(19) "2014-03-11 08:00:00"
    [5]=>
    string(19) "2014-03-11 17:00:00"
}
["data"]=>
    array(6) {
    [0]=>
    string(115) "
2 tests were booked
"
    [1]=>
    string(316) "
08:30am
Matamata Class 1 R & F
"
    [2]=>
    string(319) "
08:00am-04:00pm
Truck Course
"
    [3]=>
    string(328) "
08:00am-03:30pm
Trade Ed Rot Class 2
"
    [4]=>
    string(326) "
08:00am-03:30pm
Trade Ed Tga Class 2
"
    [5]=>
    string(330) "
05:00pm-08:00pm
Tauranga Truck Course
"
}
["ad"]=>
    array(6) {
    [0]=>
    int(1)
    [1]=>
    int(0)
    [2]=>
    int(0)
    [3]=>
    int(0)
    [4]=>
    int(0)
    [5]=>
    int(0)
}
}

这是我的职责:

function date_compare($a, $b) {
    return strtotime($a['date'])>strtotime($b['date']);
}

我正在尝试使用以下uasort uasort($dayEvents, 'date_compare');按日期/时间对所有数据进行排序,但我为每个数据获取Notice: Undefined index: date。我现在搜索了大约一个小时没有运气,有人能看到我做错了吗?

更新 好的,我已经改变了我的数组结构,但是它仍然没有按日期排序数据?

启动数组:

$dayEvents = array();
$dayEvents['output'] = array();

它的分配方式如下:

$dayEvents['output'][] = array('date' => $dateStamp, 'data' => $dataOutput, 'ad' => $allDay);

示例输出:

array(1) {
["output"]=>
  array(6) {
    [0]=>
    array(3) {
      ["date"]=>
      string(19) "2014-03-12 00:00:00"
      ["data"]=>
      string(115) "
2 tests were booked
"
      ["ad"]=>
      int(1)
    }
    [1]=>
    array(3) {
      ["date"]=>
      string(19) "2014-03-12 08:30:00"
      ["data"]=>
      string(316) "
08:30am
Matamata Class 1 R & F
"
      ["ad"]=>
      int(0)
}
    [2]=>
    array(3) {
      ["date"]=>
      string(19) "2014-03-12 08:00:00"
      ["data"]=>
      string(319) "
08:00am-04:00pm
Truck Course
"
      ["ad"]=>
      int(0)
    }
    [3]=>
    array(3) {
      ["date"]=>
      string(19) "2014-03-12 08:00:00"
      ["data"]=>
      string(328) "
08:00am-03:30pm
Trade Ed Rot Class 2
"
      ["ad"]=>
      int(0)
    }
    [4]=>
    array(3) {
      ["date"]=>
      string(19) "2014-03-12 08:00:00"
      ["data"]=>
      string(326) "
08:00am-03:30pm
Trade Ed Tga Class 2
"
      ["ad"]=>
      int(0)
    }
    [5]=>
    array(3) {
      ["date"]=>
      string(19) "2014-03-12 17:00:00"
      ["data"]=>
      string(330) "
05:00pm-08:00pm
Tauranga Truck Course
"
      ["ad"]=>
      int(0)
    }
}
}

对其进行排序的声明:

if(count($dayEvents['output'])>1) {
    uasort($dayEvents, 'date_compare');
}

功能本身:

function date_compare($a, $b) {
    return strtotime($a['date']) > strtotime($b['date']);
}

我不再收到未定义的索引错误,但它仍然没有按日期排序?谁能看到我做错了什么?

1 个答案:

答案 0 :(得分:1)

ad只是一个带零的数组...

您是否尝试对日期数组进行排序?

<?php

$dayEvents = array(
    "date" => array(
        "2014-03-12 08:30:00",
        "2014-03-10 08:00:00",
        "2014-03-11 08:00:00",
        "2014-03-11 08:00:00",
        "2014-03-11 17:00:00"
    ),
    "ad" => array(
        0,
        0,
        0,
        0,
        0
    )
);

function date_compare($a, $b) {
    return strtotime($a) > strtotime($b);
}

usort($dayEvents['date'], 'date_compare');

var_dump($dayEvents);

Output

array(2) {
  ["date"]=>
  array(5) {
    [0]=>
    string(19) "2014-03-10 08:00:00"
    [1]=>
    string(19) "2014-03-11 08:00:00"
    [2]=>
    string(19) "2014-03-11 08:00:00"
    [3]=>
    string(19) "2014-03-11 17:00:00"
    [4]=>
    string(19) "2014-03-12 08:30:00"
  }
  ["ad"]=>
  array(5) {
    [0]=>
    int(0)
    [1]=>
    int(0)
    [2]=>
    int(0)
    [3]=>
    int(0)
    [4]=>
    int(0)
  }
}