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 7778615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:26:33+00:00 2026-06-01T18:26:33+00:00

I have a dynamically generated array of filenames, let’s say it looks something like

  • 0

I have a dynamically generated array of filenames, let’s say it looks something like this:

$files = array("a-file","b-file","meta-file-1", "meta-file-2", "z-file");

I have a couple of specific filenames that I want discarded from the array:

$exclude_file_1 = "meta-file-1";
$exclude_file_2 = "meta-file-2";

So, I’ll always know the values of the elements I want discarded, but not the keys.

Currently I’m looking at a couple of ways to do this.
One way, using array_filter and a custom function:

function excludefiles($v)
        {
        if ($v === $GLOBALS['exclude_file_1'] || $v === $GLOBALS['exclude_file_2'])
          {
          return false;
          }
        return true;
        }

$files = array_values(array_filter($files,"excludefiles"));

Another way, using array_keys and unset:

$exclude_files_keys = array(array_search($exclude_file_1,$files),array_search($exclude_file_2,$files));
foreach ($exclude_files_keys as $exclude_files_key)
    {    
    unset($files[$exclude_files_key]);
    }
$files = array_values($page_file_paths);

Both ways produce the desired result.

I’m just wondering which one would be more efficient (and why)?

Or perhaps there is another, more efficient way to do this?

Like maybe there’s a way to have multiple search values in the array_search function?

  • 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-01T18:26:35+00:00Added an answer on June 1, 2026 at 6:26 pm

    You should simply use array_diff:

    $files = array("a-file","b-file","meta-file-1", "meta-file-2", "z-file");
    $exclude_file_1 = "meta-file-1";
    $exclude_file_2 = "meta-file-2";
    
    $exclude = array($exclude_file_1, $exclude_file_2);
    $filtered = array_diff($files, $exclude);
    

    One of the bad things about PHP is that it has zillions of functions to do specific little things, but that can also turn out to be convenient sometimes.

    When you come across a situation like this (you have found a solution after locating a relevant function, but you are unsure if there is something better), it’s a good idea to browse the function list sidebar on php.net at leisure. Just reading the function names can pay huge dividends.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have an array dynamically generated by keypress, then have different
I have dynamically generated strings like @#@!efq@!#! , and I want to remove specific
I have an array thats generated dynamically and it has some empty elements. How
I have a number (e.g. 6) that is dynamically generated and I would like
I have a dynamically generated array from a database. Basically it's an array of
i have a table array dynamically generated from a data query and stored in
I have an array of button which is dynamically generated at run time. I
I have dynamically generated radio buttons based on an array. The name and id
I have an array which contains many keys/values that are generated dynamically, so I
I have the following dynamically generated array: var myArray = (0% { left:74px; top:202px;

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.