dqg2269 2017-01-06 15:33
浏览 12
已采纳

搜索日期数组不会返回结果

Hello I am trying to search for an hour based on the current date with the following function:

$key = array_search($todayday, array_column($days, 'date', 'hours'));

Where $todayday is equal to today day which comes from the sever day date.

the Array $days is:

                 Array
(
    [0] => Array
        (
            [date] => 01
            [hours] => 0
        )

    [1] => Array
        (
            [date] => 02
            [hours] => 8
        )

    [2] => Array
        (
            [date] => 03
            [hours] => 16
        )

    [3] => Array
        (
            [date] => 04
            [hours] => 24
        )

    [4] => Array
        (
            [date] => 05
            [hours] => 32
        )

    [5] => Array
        (
            [date] => 06
            [hours] => 40
        )

    [6] => Array
        (
            [date] => 07
            [hours] => 40
        )
... etc....

while $todaydate is equal to 1,2,3,4,5 it works. but for 6 and 7 does not return anything. Any help is welcome to resolve this issue. Thanks

I suspect that since date = 6 and 7 have the same value of hours this is why it does not return anything but the question is how to return it actually.

  • 写回答

1条回答 默认 最新

  • doutao1282 2017-01-06 15:56
    关注

    Your supposition is correct, you can not have duplicate keys. Use the key from the search and then access hours:

    $key   = array_search($todayday, array_column($days, 'date'));
    $hours = $days[$key]['hours'];
    

    Or shorter:

    $hours = $days[array_search($todayday, array_column($days, 'date'))]['hours'];
    

    In the case where the indexes are not 0 based and sequential, use array_values() to re-index:

    $days  = array_values($days);
    $key   = array_search($todayday, array_column($days, 'date'));
    $hours = $days[$key]['hours'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)