1

I am looping between 2 times in 10 minute intervals, which works fine and outputs a time like so:

<?=$time->format('H:i')?>

I then am pulling data from the database of times I then want to see if the data in the loop matches whats coming out of the database. I have created a method to get me all the records from the database and outputs them into an array. I then wanted to use in_array to match them up then run the value through another method to get data about it. Problem is that it doesnt match up, problem being:

if (array_search($time->format('H:i'), $bookings))
echo "Match";

$booking is a multi-dimension array looking like:

Array ( 
[0] => Array ( [id] => 1 [time] => 12:00 ) 
[1] => Array ( [id] => 2 [time] => 15:00 )
...
)

Thanks in advance!

3
  • 4
    Why not modify your SQL query to give you the results already matched up to their times? Commented Jan 18, 2012 at 0:58
  • 1
    I expect it to be crawling with array_filter answers in a few minutes here... But go with Mark's suggestion. Commented Jan 18, 2012 at 1:00
  • @Wrikken Or even array_walk()! Commented Jan 18, 2012 at 1:03

2 Answers 2

1

It would be much easier if you get the values directly from database. still if you want to process it in php, you can try with array_walk(). I am not sure about the syntax but should be something like

function search($value, $key, $needle)
{
    array_search($needle, $value);
}

array_walk($bookings, 'search', $time->format('H:i'));

where $value will be your inner arrays.
Guys, please correct me if i am wrong with the syntax

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry was over complicating it completely, just wrote a simple method to get it from the database and is working perfectly, thanks though! :)
0

It would probably be simpler to directly query the database accordingly - if you can - and than your query would be something like

select * from `table_name` WHERE `date_field` = $your_date

If that does not form a solution you can use array_walk as above or simply loop a little more:

foreach($bookings as $array) {
    if(array_search($time->format('H:i'), $array)) {
        echo 'match';
        // If you don't want to keep searching use 'break'.
    }
}

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.