我正在学习PHP中的数组。我正在研究我面临问题的多维数组。我想在场地和日期打印输出基础。
以下是多维数组示例的说明:
Array
(
[0] => Array
(
[venue] => chicago
[course] => science
[time] => 2pm
[date]=>jan2016-feb-2017
)
[1] => Array
(
[venue] => newyork
[course] => robotics
[time] =>3pm
[date]=>jan2016-feb-2017
)
[2] => Array
(
[venue] => newyork
[course] => science
[time] => 4pm
[date]=>mar2017-feb-2018
)
[3] => Array
(
[venue] => chicago
[course] => robtics
[time] => 4pm
[date]=>mar2017-feb-2018
)
[4] => Array
(
[venue] => chicago
[course] => robotics
[time] => 2pm
[date]=>jan2016-feb-2017
)
[5] => Array
(
[venue] => chicago
[course] => science
[time] => 4pm
[date]=>mar2017-feb-2018
)
[6] => Array
(
[venue] => newyork
[course] => robotics
[time] =>1pm
[date]=>mar2017-feb-2018
)
[7] => Array
(
[venue] => newyork
[course] => science
[time] => 5pm
[date]=>mar2017-feb-2018
)
)
这是我想要的所需输出:
chicago
Date:jan2016-feb-2017
science 2pm
robotics 2pm
chicago
Date:mar2017-feb-2018
science 4pm
robotics 4pm
newyork
Date:jan2016-feb-2017
science 4pm
robotics 3pm
newyork
Date:mar2017-feb-2018
science 5pm
robotics 1pm
我想打印场地和日期的输出基础。请帮助我们
我目前的代码:
usort($events, function($a, $b) {
return strcmp($a['venue'], $b['venue']);
});
$previousVenue = null;
foreach ($events as $event) {
// check if this venue is the same as the previous, output header if not
if ($event['venue'] != $previousVenue) {
echo "<h3>$event[venue]</h3>";
echo "$event[date]";
}
// always output course and time
echo "$event[course] $event[time]<br>";
// current venue becomes previous venue
$previousVenue = $event['venue'];
}
我设法用场地搜索它,但我无法按照日期打印它。
答案 0 :(得分:0)
您可以使用usort或uasort根据一个或多个键对多维数组进行排序。例如。你可以分别根据场地和日期对它进行排序,然后迭代结果并打印出来。
uasort($events, function($a, $b) {
$venueComparison = strcmp($a['venue'], $b['venue']);
if (!$venueComparison) {
return compare_date($a['date'], $b['date']);
}
});
foreach ($events as $event) {
echo '...';
}
或者你可以迭代多维数组并将其转换为更合适的结构。
$groupedEvents = array();
foreach ($events as $event) {
if (!array_key_exists($event['venue'], $groupedEvents)) {
$groupedEvents[$event['venue']] = array();
}
$groupedEvents[$event['venue']][] = $event;
}
foreach ($groupedEvents as $venue => $events) {
usort($events, 'compare_date');
foreach ($events as $event) {
echo '...';
}
}
无论哪种方式,您都需要一些能够解析日期范围并对其进行排序的东西。您将需要自己进行解析,然后决定是否要按开始日期或结束日期排序。
或者,如果可能,您可以切换到更容易排序的日期范围格式。例如。 ISO 8601.
一次是在日期的第一部分拆分strotime(),然后只比较生成的时间戳。像这样:
function compare_date($a, $b) {
return strtotime(substr($a, 0, strpos($a, '-'))) - strtotime(substr($b, 0, strpos($b, '-')));
}
答案 1 :(得分:0)
您需要将日期设为我们可以“订购”的内容。如果你这样做,你可以再次运行usort。 (php7方式顺便说一下)。希望这有用...编辑,有点凌乱的代码,但也许你得到它:)干杯
/*-----( example array )------*/
$arr = array(
array(
'venue' => 'chicago',
'course' => 'science',
'time' => '2pm',
'date' => 'jan2016-feb-2017',
),
array(
'venue' => 'amsterdam',
'course' => 'science',
'time' => '4pm',
'date' => 'jan2016-feb-2017',
),
array(
'venue' => 'chicago',
'course' => 'science',
'time' => '2pm',
'date' => 'jan2016-feb-2017',
),
);
/*-----( remove dublicates )-----*/
$s = array_map('serialize', $arr);
$u = array_unique($s);
$arr = array_intersect_key($arr, $u);
/*-----( sort based on venue)------*/
usort($arr, function($a, $b) {
return $a['venue'] <=> $b['venue'];
});
/*----( edit, order on dates )----*/
foreach($arr as $k => $a)
{
$t = explode('-',$a['date']);
$arr[$k]['t_date'] = isset($t[0]) ? $t[0] :null;
}
usort($arr, function($a, $b) {
return $a['t_date'] <=> $b['t_date'];
});
/*----( output )----*/
foreach($arr as $a)
{
foreach($a as $k => $v)
{
echo $k == 'venue' ? $v : $k.' '.$v;
echo '<br>';
}
echo '<br>';
}