laravel PHP -从数组中获取最接近的日期

kuuvgm7e  于 2022-12-01  发布在  PHP
关注(0)|答案(3)|浏览(192)

如果与$array ['date ']中值不相同,我想从$search_date中获取最接近日期如果与$array ['date']中值相同,我想获取所有数组

  • 日期格式为'Y-m-d'。

例一:

$search_date = '2022-12-08';
$array = [{"price":"200","date":"2022-12-12"},{"price":"50","date":"2022-12-10"},{"price":"100","date":"2022-12-10"}]
Return should be: [{"price":"50","date":"2022-12-10"},{"price":"100","date":"2022-12-10"}]

例二:

$search_date = '2022-12-08';
$array = [{"price":"200","date":"2022-12-08"},{"price":"50","date":"2022-12-09"},{"price":"100","date":"2022-12-11"}]
Return should be: [{"price":"200","date":"2022-12-08"}]

例三:

$search_date = '2022-12-08';
$array = [{"price":"200","date":"2022-12-10"},{"price":"100","date":"2022-12-10"},{"price":"50","date":"2022-12-11"}]
Return should be: [{"price":"200","date":"2022-12-10"},{"price":"100","date":"2022-12-10"}]

例四:

$search_date = '2022-12-08';
$array = [{"price":"200","date":"2022-12-08"},{"price":"100","date":"2022-12-08"},{"price":"50","date":"2022-12-08"}]
Return should be: [{"price":"200","date":"2022-12-08"},{"price":"100","date":"2022-12-08"},{"price":"50","date":"2022-12-08"}]

谢谢你,谢谢你

knpiaxh1

knpiaxh11#

此代码计算$search与每条记录之间的距离(以天为单位)。它假定您要查找未来和过去的最近距离。

<?php

/*

Question Author: Catalin Iamandei
Question Answerer: Jacob Mulquin
Question: PHP - get closest date from array
URL: https://stackoverflow.com/questions/74598442/php-get-closest-date-from-array
Tags: php, arrays, laravel, date, php-carbon

*/

$search = '2022-12-10';
$searchObj = new DateTime($search);

$records = json_decode('[{"price":"200","date":"2022-12-10"},{"price":"100","date":"2022-12-10"},{"price":"50","date":"2022-12-11"}]', true);

$distances = [];
foreach ($records as $index => $record) {
    $recordObj = new DateTime($record['date']);
    $daysDiff = $searchObj->diff($recordObj)->format("%r%a");
    $distances[$index] = abs($daysDiff);
}

$minimumDiff = min($distances);

$output = [];
foreach ($distances as $index => $distance) {
    if ($distance == $minimumDiff) {
        $output[] = $records[$index];
    }
}

echo json_encode($output, JSON_PRETTY_PRINT);

产量:

[
    {
        "price": "50",
        "date": "2022-12-09"
    },
    {
        "price": "100",
        "date": "2022-12-11"
    }
]

如果您只想搜索将来最接近的日期,则需要删除abs()函数,然后在使用min()之前删除$distances数组中的所有负项。

5sxhfpxr

5sxhfpxr2#

如果要在数组中搜索特定值,您是否尝试过

array_search($value, $array);

通过这个你可以在数组中搜索一个特定的值
如果要搜索最低值
尝试对数组进行for循环,检查数组是否小于前一个索引,如果for循环已经结束,则得到最小的日期

$lowest_date = null;

for ($i = 0; count($i); $i++) {
    if ($array['date'] < $lowest_date) {
        $lowest_date = $array['date'];
    }
}
yhqotfr8

yhqotfr83#

您没有提到如何处理之前的日期,例如,搜索'2022-12-07',如何处理2022-12-062022-12-08,因为两者的差都是1。您可以计算每个条目的datediff,获取最小datediff并使用此datediff输出元素。例如:

<?php
$SearchDate = new DateTimeImmutable('2022-12-08');
$array      = array ('{"price":"200","date":"2022-12-12"}',
    '{"price":"50","date":"2022-12-10"}',
    '{"price":"100","date":"2022-12-10"}');
$laResult = array();
foreach($array as $jsonO) {
    $json             = json_decode($jsonO);
    $CompareDate      = new DateTimeImmutable($json->{'date'});
    $interval         = date_diff($SearchDate, $CompareDate);
    $laThis['date']   = $json->{'date'};
    $laThis['diff']   = $interval->format('%a');
    $laThis['origin'] = $jsonO;
    $laResult[]       = $laThis;
}

$min_diff = min( array_column( $laResult, 'diff') );
echo 'nearestDiff:'. $min_diff .PHP_EOL;
foreach($laResult as $laSingleResult) {
    if($laSingleResult['diff'] == $min_diff) {
        echo $laSingleResult['origin'] .PHP_EOL;
    }
}

相关问题