php多维数组获取第一个日期值

发布于 2024-11-04 23:57:49 字数 1269 浏览 1 评论 0原文

我再次需要帮助。我有一个数组,我需要提取最早一天的体重值。

编辑 - 编辑 - 编辑


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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无所的.畏惧 2024-11-11 23:57:49

您可以使用 usort() 使用自定义回调对数组进行排序,然后取第一个元素。

// $arr is your array
usort($arr, 'sortByDate');
$earliest = $arr[0];

function sortByDate ($arr1, $arr2)
{
   return strcmp($arr1['Date'], $arr2['Date']);
}

You could sort the array with a custom callback using usort() and then take the first element.

// $arr is your array
usort($arr, 'sortByDate');
$earliest = $arr[0];

function sortByDate ($arr1, $arr2)
{
   return strcmp($arr1['Date'], $arr2['Date']);
}
南薇 2024-11-11 23:57:49

这是一种方法:

function filter_min($a, $b) {
  return ($a['Date'] < $b['date']) ? $a : $b;
}

$result = array_reduce($array, 'filter_min');
echo $result['Weight'];

另一种方法是简单地迭代数组并找到最小的日期。

This is one way of doing it:

function filter_min($a, $b) {
  return ($a['Date'] < $b['date']) ? $a : $b;
}

$result = array_reduce($array, 'filter_min');
echo $result['Weight'];

Another way is to simply iterate over the array and find the smallest date.

转角预定愛 2024-11-11 23:57:49
$smallest = null; // array index of entry with smallest weight.
foreach ($array as $idx => $data) {
    if (($data['Weight'] < $array[$smallest]['Weight']) || (is_null($smallest))) {
      $smallest = $idx; // found a small weight, so save the index
    }
}

echo $array[$smallest]['Date'];
$smallest = null; // array index of entry with smallest weight.
foreach ($array as $idx => $data) {
    if (($data['Weight'] < $array[$smallest]['Weight']) || (is_null($smallest))) {
      $smallest = $idx; // found a small weight, so save the index
    }
}

echo $array[$smallest]['Date'];
慢慢从新开始 2024-11-11 23:57:49

我注意到日期是相反的顺序,最晚的日期首先被拉入,最早的日期最后被拉入。会一直这样吗?如果是这样,您可以这样做:

$index = count($array) - 1;
$earliestdate = $array[$index]['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:

$index = count($array) - 1;
$earliestdate = $array[$index]['Date'];

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 it ASC that would get you what you want, can't remember)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文