dpw30157 2015-01-29 17:19
浏览 21

PHP匹配数组中的3个键

Hi I've been struggling with the most efficient way of doing this. I have an array like this

array(3) {
  [0]=>
  object(stdClass)#495 (4) {
    ["year"]=>
    string(4) "2015"
    ["month"]=>
    string(1) "1"
    ["dayofmonth"]=>
    string(2) "29"
    ["posts"]=>
    string(1) "3"
  }
  [1]=>
  object(stdClass)#521 (4) {
    ["year"]=>
    string(4) "2015"
    ["month"]=>
    string(1) "1"
    ["dayofmonth"]=>
    string(2) "28"
    ["posts"]=>
    string(1) "2"
  }
  [2]=>
  object(stdClass)#522 (4) {
    ["year"]=>
    string(4) "2015"
    ["month"]=>
    string(1) "1"
    ["dayofmonth"]=>
    string(2) "25"
    ["posts"]=>
    string(1) "1"
  }
}

And I have the following values

$d = 29, $m = 1, $y = 2015

What I want to do is find which item in the array [0,1,2] is the next date before the date given by my $d, $m and $y variables.

For example, the answer below would be [1]

If the date was $d=28 then it would be [2]

If the day was $d=25 then it would return false.

The issue is the array could cover a number of years so it needs to match exactly all 3 keys and then return the key number after the found key. The array will always be in descending date order

Is this possible?

EDIT:

I am trying to query a list of items on a date basis so it will show

29 JAN 2015

Click NEXT

It will show the next day which if there is an item will just be day minus 1 but if not it will be the day minus [x]

So the list could be

29 JAN 2015
{items}
28 JAN 2015
{items}
25 JAN 2015
{items}
10 DEC 2014
{items}
29 JAN 2014
{items}
...
25 JAN 1999
{items}

and I want to be able to have this so it just shows the most up to date, then someone can click to show the next dates bunch of items.

If there's an easier way to do this via a dynamic mySQL it would be useful

FURTHER EDIT

I can extract just the date too

array(3) {
  [0]=>
  object(stdClass)#495 (2) {
    ["post_date"]=>
    string(19) "2015-01-29 14:23:28"
    ["posts"]=>
    string(1) "3"
  }
  [1]=>
  object(stdClass)#521 (2) {
    ["post_date"]=>
    string(19) "2015-01-28 14:24:05"
    ["posts"]=>
    string(1) "2"
  }
  [2]=>
  object(stdClass)#522 (2) {
    ["post_date"]=>
    string(19) "2015-01-25 14:41:43"
    ["posts"]=>
    string(1) "1"
  }
}

With the view to select the first [20] number of posts and then "next" would load in the next days worth of posts

  • 写回答

3条回答 默认 最新

  • doujing6053 2015-01-29 17:25
    关注

    If your array is sorted then it's actually pretty easy, just loop in reverse order until your search is greater than the current value

    function s($array, $y,$m,$d) {
        $array = array_reverse($array);
        $search = new DateTime( "$y-$m-$d" );
        foreach($array as $k=>$v) {
            $current = new DateTime( "{$v->year}-{$v->month}-{$v->dayofmonth}" );
            if ( $current >= $search )
              return $k-1;
        }
        // otherwise return false, you didn't find any correct date
        return false;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 vscode的问题提问
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM