2

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 Answer 1

2

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'];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.