Nested foreach with php


Nested foreach with php

我面临的问题是,当我在另一个foreach中使用一个foreach时,第一个foreach的数组有多个条目。我想做的是从数组2中排除数组1的所有条目。我几乎上过所有相关的帖子,无法自己解决,如果可能的话,我需要一点帮助。抱歉我英语不好。

示例:

$choice-->每次具有随机数目的条目的数组(对于本例为2)

示例:

/var/www/clients/client1/web1/web/images,/var/www/clients/client1/web1/web/tmp

$list-->每次的随机条目数数组(本例为10000)

示例:

/var/www/clients/client1/web1/web/images,/var/www/clients/client1/web1/web/tmp,/var/www/clients/client1/web1/web/includes,/var/www/clients/client1/web1/web/libraries,......

$list的条目总是多于$choice

我这里有这个代码:

foreach ( $choice as $select )
{
    foreach ( $list as $file )
    {
        if ( (strpos( $file, $select )) !== false ) 
        {
            // exclude this
        }
        else
        {
            // include this
        }
    }
}

上面的代码会做什么(不幸的是)是:

步骤1。将比较$select条目-1与所有$file条目。

步骤2。将从所有$file条目中排除$select条目-1,并将包括$select条目-2。

步骤3。将比较$select条目-2与所有$file条目。

步骤4。将从所有$file条目中排除$select条目2,并将包括$select条目1。

结果:没有排除任何内容。

非常感谢您的帮助。我已经做了一个星期了,我所做的只是把它们从里到外——我没有主意了。非常感谢。

我相信您正试图从$choice中删除$list中的项目。(或者相反?)你试过array_diff函数吗?如果两个数组中的项相等,这将起作用。例如:

<?php
//Option 1: array_diff
$bigger = array("A", "B", "C", "D", "E", "F");
$smaller = array("A", "B");
$result = array_diff($bigger, $smaller);
print_r($result);

如果需要对删除的项进行额外处理,可以尝试in_array,但这需要项相等(如上所述)。例如:

//Option 2: in_array (only one foreach loop)
foreach ($smaller as $key => $item) {
    if (in_array($item, $bigger)) {
        //do something to "remove" it, for example:
        unset($smaller[$key]);
        unset($bigger[$key]);
        //...
    }
}
print_r($smaller);
print_r($bigger);

最后,如果不能保证两个数组中的项严格相等,则可以使用双foreach。您需要在内部循环中标记项目,并在外部循环中处理它们。例如:

//Option 3: double-foreach (items not strictly equals)
$choice = array(
    "/var/www/clients/client1/web1/web/images",
    "/var/www/clients/client1/web1/web/tmp"
);
$list = array(
    "/var/www/clients/client1/web1/web/images",
    "/var/www/clients/client1/web1/web/tmp",
    "/var/www/clients/client1/web1/web/includes",
    "/var/www/clients/client1/web1/web/libraries",
    // more items
);

foreach ($choice as $choice_key => $choice_item) {
    $exists_in_list = FALSE;
    foreach ($list as $list_key => $list_item) {
        if (strpos($list_item, $choice_item) !== FALSE) {
            //$choice_item is string-contained inside $list_item:
            $exists_in_list = TRUE;
            //Do some processing on $list (while "$list_key" is in scope). For example:
            unset($list[$list_key]); //removes the matching items from $list
            //...
            break;
        }
    }
    if ($exists_in_list) {
        //Do post-processing on $choice. For example:
        unset($choice[$choice_key]); //removes the matching items from $choice
        //...
    }
}
echo '$choice is now ';
print_r($choice);
echo '$list is now ';
print_r($list);

$结果是:

//Option 1:
Array //$result == $bigger - $smaller
(
    [2] => C
    [3] => D
    [4] => E
    [5] => F
)
//Option 2:
Array //$smaller - $bigger
(
)
Array //$bigger - $smaller
(
    [2] => C
    [3] => D
    [4] => E
    [5] => F
)
//Option 3:
$choice is now Array
(
)
$list is now Array
(
    [2] => /var/www/clients/client1/web1/web/includes
    [3] => /var/www/clients/client1/web1/web/libraries
)