虽然数组中包含多个值的语句 [英] While statment with multiple values contained in an array

查看:212
本文介绍了虽然数组中包含多个值的语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,每个条目包含几个信息。我使用while语句遍历每一个并显示我称之为plots的数字,每个条目可以包含多个图表EG 111,222,333

I have a database that holds several bits of info per entry. I use a while statement to loop through each one and display what I call "plots" which will be a number, each entry can hold multiple plots EG 111, 222, 333

我需要做什么我有while语句(或者可能更适合的其他方法)来检查每个条目保存的绘图编号是否包含在我设置的数组中然后显示信息
EG:

what I need to do I have the while statement (or another method that may suit better) to check whether that plot numbers that each entry holds are contained within a array that I set and then display so information EG:

while ($row = mysqli_fetch_array($aa, MYSQLI_ASSOC)) {

    $plotsTotal1 = explode(" ", $row['exoutdoorspaceplottotals']);
    $plotsTotal2 = array_filter($plotsTotal1); 
    //this gets info from DB. an example of the data would be:
    //$plotsTotal2 = array(111, 555);

    $test = array(111, 465, 555, 666, 777);

    if (count(array_intersect($plotsTotal2, $test)) > 0) {
     echo 'something'; <br/>
    }
}

所以$ plotsTotal2是3个阵列的组合。

So $plotsTotal2 Is a combination if 3 arrays.

作为一个例子如果我有3个托管,则while语句将循环。
条目有一个地块,111,222,465
条目二有地块,666,123,412
条目三有地块,000,999

As an example If I have 3 entrys the the while statment will loop. entry one has plots, 111, 222, 465 entry two has plots, 666, 123, 412 entry three has plots, 000, 999

while循环应该显示一些信息(可能是每个条目的名称和图),结果将是:

The while loop should display some information for (Probably the name and plots for each entry) so the outcome would be:

条目一111,222,465
第二项666,123,412

Entry one 111,222,465 Entry two 666, 123, 412

(第三项没有匹配的数字,因此没有显示任何内容)

(entry three doesn't have matching numbers so therefore not displayed anything)

任何合适的帮助,希望我解释这一切都好。

Any help appropriated, hope i explained this all ok.

Ian

推荐答案

这样的东西? 在此处测试这虽然版本

$entries = array(
    "111 222 465",
    "666 123 412",
    "000 999",
);

$test = array(111, 465, 555, 666, 777);

foreach ($entries as $key => $entry) {
    $plotsTotal1 = explode(' ', $entry);
    $plotsTotal2 = array_filter($plotsTotal1);

    $matches = array_intersect($test, $plotsTotal2);

    if (count($matches) > 0) {
        echo 'Entry ' . $key. ' contains ' . implode(',', $matches) . "\n";
    }
}

这篇关于虽然数组中包含多个值的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆