简体   繁体   English

PHP多维数组中的匹配值

[英]PHP Matching value in an multi-dimensional array

I am looping between 2 times in 10 minute intervals, which works fine and outputs a time like so: 我以10分钟为间隔在2次之间循环播放,效果很好,并输出如下时间:

<?=$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. 然后,我想使用in_array进行匹配,然后通过另一种方法运行该值以获取有关它的数据。 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: $ booking是一个多维数组,看起来像:

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

Thanks in advance! 提前致谢!

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(). 仍然,如果您想在php中处理它,则可以尝试使用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. 其中$value将是您的内部数组。
Guys, please correct me if i am wrong with the syntax 伙计们,如果我在语法上有误,请纠正我

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: 如果这不能解决问题,则可以使用上面的array_walk或简单地循环一些:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM