Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8338951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:44:56+00:00 2026-06-09T04:44:56+00:00

The problem I am facing is when I use one foreach inside another and

  • 0

The problem I am facing is when I use one foreach inside another and the array of the first foreach has more than 1 entries. What I want to do is to exclude all entries of array 1 from array 2. I’ve been on almost all related posts, cannot solve it by myself, I need a little help if possible. Sorry for my bad English.

Example:

$choice —> array with random number of entries each time (for this example 2)

Example:

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

$list —> array of random number of entries each time (for this example 10000)

Example:

/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 has always more entries than $choice

And I have this code here:

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

What the above code will do (unfortunately) is:

Step 1. Will compare $select entry-1 with all $file entries.

Step 2. Will exclude $select entry-1 from all $file entries and will include the $select entry-2.

Step 3. Will compare $select entry-2 with all $file entries.

Step 4. Will exclude $select entry-2 from all $file entries and will include the $select entry-1.

Result:
Nothing excluded.

Any help truly appreciated. I am on this for like a week, all I have tried is putting them inside out I am out of ideas.
Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:45:00+00:00Added an answer on June 9, 2026 at 4:45 am

    I believe you’re trying to remove items that are in $list from $choice. (Or is it the other way around?) Have you tried the array_diff function? This will work if items in both array are equal. For example:

    <?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);
    

    If you need to do additional processing on the removed items, you can try in_array, but this requires item equality (like above). For example:

    //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);
    

    Lastly, if the items in both arrays are not guaranteed to be strictly equals, you could use a double foreach. You’ll need to flag items in the inner loop and process them in the outer loop. For example:

    //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);
    

    The $result is:

    //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
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use JSP page to return dynamic xml.The problem I'm facing is
I am facing problem. I need to build one app in two ways, first
Iam facing problem in understanding and converting a matlab code into opencv. I want
iam facing problem in passing array to view. this is my controller code.. function
I'm facing a problem using ExtJS4. My application has a tree containing a string
These days I'm facing a problem with encoding string from an array of byte.
I'm facing the problem of merging a subset of revisions from one topic-branch to
I am facing the problem of low memory. Low memory:no more background process And
I need to use asp:CustomValidator for a tool. The problem am facing is that
I am facing one problem in Tapestry. Problem - I am using a grid

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.