从符合条件的行中获取多维数组中最早的 Y-m-d 日期值

Get earliest Y-m-d date value in a multidimensional array from qualifying rows

我想从多维数组的日期列中获取最小日期,但只能从 keycode0001 的行中获取。

我将如何实现?

$a = [
  "a" => ['keycode' => '0001','date' => "2015-09-08"],
  "b" => ['keycode' => '0002','date' => "2015-09-05"],
  "c" => ['keycode' => '0003','date' => "2015-09-04"],
  "d" => ['keycode' => '0001','date' => "2015-09-01"],
  "e" => ['keycode' => '0001','date' => "2015-08-01"]
];

预期结果:2015-08-01

这会起作用:

function min_date($a){
 $dates = [];
 foreach($a as $key => $value){
 if($value['keycode']=='0001'){
    array_push($dates,strtotime($value['date']));
  }
 }
 return date("Y/m/d",min($dates));
}

$result = min_date($a);

我会首先提取键码为 0001 的所有值,并创建一个仅包含这些日期的新数组。然后使用phpmin()函数。

PHP Fiddle

$a=array(
  "a" => array('keycode' => '0001','date' =>"2015-09-08"),
  "b" => array('keycode' => '0002','date' =>"2015-09-05"),
  "c" => array('keycode' => '0003','date' =>"2015-09-04"),
  "d" => array('keycode' => '0001','date' =>"2015-09-01"),
  "e" => array('keycode' => '0001','date' =>"2015-08-01")
);

$b = [];

foreach($a as $key => $value){
    if($value['keycode']=='0001'){
     $b[]=$value['date'];
    }
}

print_r($b);

echo min($b);

您可以结合使用 array_filter and uasort:

function getMinDateByKeycode(array $a, $keycode)
{
    // Filter out elements by keycode
    $aFiltered = array_filter($a, function($aItem) use ($keycode) {
        return $aItem['keycode'] === $keycode;
    });

    // Sort lowest date first
    uasort($aFiltered, function($a1, $a2){
        return strtotime($a1['date']) - strtotime($a2['date']);
    });

    // Get the key of the first element of the resulting array
    reset($aFiltered);
    $keyOfMinDate = key($aFiltered);

    // Return it however you want. This example returns just the date.
    return $a[$keyOfMinDate]['date'];
}

echo getMinDateByKeycode($a, '0001');
// prints '2015-08-01'

如果数组已经排序并且您使用的是php 5.5.0+,那么您可以试试这个

$arr = array_column($a, "date", "keycode");
echo $arr["0001"] // string(10) "2015-08-01"

因为您的日期表达式已经在 Y-m-d 中,所以它们可以立即与简单字符串进行比较。这意味着不需要为此任务的任何部分调用 strtotime()

如果您更喜欢usort(),您可以根据两个标准进行排序。以下代码段将首先对 keycode 进行排序,并将所有 0001 行放在前面,将其他所有内容放在数组的后面。当需要平局时,第二个排序规则是比较日期 ASC。

完成后,第一个出现的行将是具有最小日期值的 0001 行,或者它不会是 0001 行,因为输入数组中没有任何行.

代码:(Demo)

$array = [
  "a" => ['keycode' => '0001','date' => "2015-09-08"],
  "b" => ['keycode' => '0002','date' => "2015-09-05"],
  "c" => ['keycode' => '0003','date' => "2002-12-04"],
  "d" => ['keycode' => '0001','date' => "2015-09-01"],
  "e" => ['keycode' => '0001','date' => "2015-10-01"]
];

$needle = '0001';

usort($array, fn($a, $b) => [$a['keycode'] !== $needle, $a['date']] <=> [$b['keycode'] !== $needle, $b['date']]);

echo $array[0]['keycode'] === $needle ? $array[0]['date'] : "No qualifying rows for $needle";
// 2015-09-01

如果您希望看到经典循环,那么此方法将有条件地更新临时变量。 (Demo)

$needle = '0001';

$earliest = null;
foreach ($array as $row) {
    if ($row['keycode'] === $needle && (!$earliest || $row['date'] < $earliest)) {
        $earliest = $row['date'];
    }
}
echo $earliest ?? "No qualifying rows for $needle"; // 2015-09-01