your structure is array of associated arrays. To sort by key value, you need to provide your custom comparison function that takes two arrays then compare between them.
You can visit Array Sorting to see all PHP sorting functions.
I believe you need to use usort.
In the documentation example, there is function that can help you to achieve what you need.
function build_sorter($key) {
return function ($a, $b) use ($key) {
return strnatcmp($a[$key], $b[$key]);
};
}
This function will take a key as parameter, and it will return a comparison function that will compare the values associated with that key.
Copy the function to your code then use it to sort your array.
// This will sort the array based on start_date descending.
usort($rows, build_sorter('start_date'));
// This will sort the array based on start_date descending.
usort($rows, build_sorter('end_date'));
//Call the function before your table and sort the array as you which.
Update:
Here is sample code, please note when I wrote the code I used start_date
and end_date
as field name instead of date_start
and date_end
. Also please avoid using old PHP syntax.
<?php
//the function sorting function for usort
function build_sorter($key) {
return function ($a, $b) use ($key) {
return strnatcmp($a[$key], $b[$key]);
};
}
//sample data
$rows = array(
[
'id' => '1',
'start_date' => '2021-01-27',
'end_date' => '2021-02-28'
],
[
'id' => '2',
'start_date' => '2020-01-28',
'end_date' => '2020-02-28'
],
[
'id' => '3',
'start_date' => '2020-01-01',
'end_date' => '2020-01-10'
],
);
//Today dates as mentioned in the comments
$today = date('Y-m-d',time());
?>
<table>
<thead>
<tr>
<td>
id
</td>
<td>
start date
</td>
<td>
end date
</td>
</tr>
</thead>
<tbody>
<?php
//first table sort by end_date
usort($rows,build_sorter('end_date'));
//now your array is sorted you need to create the table
//for loop throw the rows to print and for filttering
foreach ($rows as $row){
if ($today < $row['end_date'] && $today > $row['start_date']){
echo "<tr><td>{$row['id']}</td><td>{$row['start_date']}</td><td>{$row['end_date']}</td><tr>";
}
}
?>
</tbody>
</table>
<table>
<thead>
<tr>
<td>
id
</td>
<td>
start date
</td>
<td>
end date
</td>
</tr>
</thead>
<tbody>
<?php
//second table sort by start_date
usort($rows,build_sorter('start_date'));
//now your array is sorted you need to create the table
//for loop throw the rows to print and for filttering
foreach ($rows as $row){
if ($today > $row['end_date'] && $today > $row['start_date']){
echo "<tr><td>{$row['id']}</td><td>{$row['start_date']}</td><td>{$row['end_date']}</td><tr>";
}
}
?>
</tbody>
</table>
<table>
<thead>
<tr>
<td>
id
</td>
<td>
start date
</td>
<td>
end date
</td>
</tr>
</thead>
<tbody>
<?php
//third table sort by end_date
usort($rows,build_sorter('end_date'));
//now your array is sorted you need to create the table
//for loop throw the rows to print and for filttering
foreach ($rows as $row){
if ($today < $row['end_date'] && $today < $row['start_date']){
echo "<tr><td>{$row['id']}</td><td>{$row['start_date']}</td><td>{$row['end_date']}</td><tr>";
}
}
?>
</tbody>
</table>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…