php多维数组获取第一个日期值
我再次需要帮助。我有一个数组,我需要提取最早一天的体重值。
编辑 - 编辑 - 编辑
array (
3 =>
array (
'id' => '20110211',
'Date' => '2011-02-11',
'Weight' => '195',
),
4 =>
array (
'id' => '20110213',
'Date' => '2011-02-13',
'Weight' => '160',
),
6 =>
array (
'id' => '20110310',
'Date' => '2011-03-10',
'Weight' => '200',
),
12 =>
array (
'id' => '20110301',
'Date' => '2011-03-01',
'Weight' => '55',
),
21 =>
array (
'id' => '20110215',
'Date' => '2011-02-15',
'Weight' => '120',
),
25 =>
array (
'id' => '20110322',
'Date' => '2011-03-22',
'Weight' => '250',
),
)
我已经编辑了这个并且此代码有效:
function sortByDate ($arr1, $arr2)
{
return strcmp($arr1['Date'], $arr2['Date']);
}
// $arr is your array
usort($weight_tracker, 'sortByDate');
$earliest = $weight_tracker[0]['Weight'];
echo $earliest;
但是由于我在此页面上有一个表单,它在更新数组时更新数组 - 我收到消息 中使用字符串偏移量作为数组
致命错误:无法在EDIT -> 我已将其重新声明为字符串,因此出现错误!使用全局和包含时要小心,因为一切都可能变得一团糟! PHP 是宽容的,但是这种“宽容”可能会在以后花费大量时间...:)
谢谢,
彼得
I need help once again. I have an array and I need to extract earliest day weight value.
EDIT - EDIT - EDIT
array (
3 =>
array (
'id' => '20110211',
'Date' => '2011-02-11',
'Weight' => '195',
),
4 =>
array (
'id' => '20110213',
'Date' => '2011-02-13',
'Weight' => '160',
),
6 =>
array (
'id' => '20110310',
'Date' => '2011-03-10',
'Weight' => '200',
),
12 =>
array (
'id' => '20110301',
'Date' => '2011-03-01',
'Weight' => '55',
),
21 =>
array (
'id' => '20110215',
'Date' => '2011-02-15',
'Weight' => '120',
),
25 =>
array (
'id' => '20110322',
'Date' => '2011-03-22',
'Weight' => '250',
),
)
I've edited this and this code works:
function sortByDate ($arr1, $arr2)
{
return strcmp($arr1['Date'], $arr2['Date']);
}
// $arr is your array
usort($weight_tracker, 'sortByDate');
$earliest = $weight_tracker[0]['Weight'];
echo $earliest;
But since I have a form on this page which updates the array when array is updated - I got message Fatal error: Cannot use string offset as an array in
EDIT -> I've re-declared this as string, hence the ERROR ! be careful when using global and includes as everything can become a mess ! PHP is forgiving, but that "forgiveness" can cost a lot of time later on... :)
Thanks,
Peter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
usort()
使用自定义回调对数组进行排序,然后取第一个元素。You could sort the array with a custom callback using
usort()
and then take the first element.这是一种方法:
另一种方法是简单地迭代数组并找到最小的日期。
This is one way of doing it:
Another way is to simply iterate over the array and find the smallest date.
我注意到日期是相反的顺序,最晚的日期首先被拉入,最早的日期最后被拉入。会一直这样吗?如果是这样,您可以这样做:
您还可以使用 array_reverse() 来反转数组并使第一个元素成为以前的最后一个元素。
如果这是从 MySQL 中提取的,您还可以将查询更改为
ORDER BY 'Date' DESC
(或者是ASC
可以得到您想要的,可以'不记得了)I noticed that the dates are in reverse order with the latest date being pulled in first, and the earliest date last. Will it always be like that? If so, you can do this:
You could also use
array_reverse()
to invert the array and make the first element the formerly last element.If this is being pulled in from MySQL you could also alter the query to
ORDER BY 'Date' DESC
(or is itASC
that would get you what you want, can't remember)