Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
4  
Why not modify your SQL query to give you the results already matched up to their times? –  Mark Baker Jan 18 '12 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. –  Wrikken Jan 18 '12 at 1:00
    
@Wrikken Or even array_walk()! –  Eugen Rieck Jan 18 '12 at 1:03
add comment

3 Answers

up vote 1 down vote accepted

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

share|improve this answer
    
Sorry was over complicating it completely, just wrote a simple method to get it from the database and is working perfectly, thanks though! :) –  Matt Pawley Jan 19 '12 at 0:18
add comment

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'.
    }
}
share|improve this answer
add comment

For one dimensional arrays, your can use in_array():

$booking = array(
    '12:00',
    '15:00'
);
var_dump(
    in_array('12:00', $booking),
    in_array('13:00', $booking)
);

It should also work with one-dimensional associative arrays:

$booking = array(
    1 => '12:00',
    2 => '15:00'
);
var_dump(
    in_array('12:00', $booking),
    in_array('13:00', $booking)
);

And you can always use foreach to iterate your array:

$booking = array(
    array('id' => 1, 'time' => '12:00'),
    array('id' => 2, 'time' => '15:00')
);
$id = NULL; 
foreach($booking as $row) {
    if ($row['time'] == '12:00') {
        $id = $row['id'];
        break;
    }
}
var_dump($id);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.