While statment with multiple values contained in an array

旧街凉风 提交于 2020-01-07 08:09:07

问题


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

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/>
    }
}

So $plotsTotal2 Is a combination if 3 arrays.

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

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

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


回答1:


Something like this? Tested here or this while version

$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";
    }
}


来源:https://stackoverflow.com/questions/23425840/while-statment-with-multiple-values-contained-in-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!