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

  • Home
  • SEARCH
  • 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 5999417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:32:56+00:00 2026-05-23T00:32:56+00:00

I have a situation where I have two arrays and I use one array(A)

  • 0

I have a situation where I have two arrays and I use one array(A) to determine which elements of the second array(B) I need to use. Since I will be combing B multiple times and each time I will not need the previously used elements I have written a function that I call sift_array.

function sift_array(&$array, $key, $value){
   foreach($array as $element){
      if($element[$key] == $value){$temp[] = $element;}
      else{$temp2[] = $element;}
   }
   $array = $temp2;
   return $temp;
}

I am curious if there is a faster way to perform this function.

Here is an example of how I use this function.

In my web app users can set up a ‘bot’ to perform an action so many times while they are away/logged off. I have a program that will run periodically to operate the bots. The bots can not run the same action if it was the last bot to do so.

The bots array has 3 key elements : account_id, action_id, job_id
The job array has 1 key element : job_id.

I pass in the bots array then for the $key I pass ‘job_id’ and for the $value I pass the job id I am working with, say job 1.

$runTheseBots = sift_array($bots, 'job_id', 1);
executeBots($runTheseBots);

This will have a faster execution time because all the bots that deal with job 1 are already handled.

$runTheseBots = sift_array($bots, 'job_id', 2); 
executeBots($runTheseBots);
  • 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-05-23T00:32:57+00:00Added an answer on May 23, 2026 at 12:32 am

    There are plenty of ways to implement this, but I’ll just comment on how you’ve done it:

    function sift_array(&$array, $key, $value){
       foreach($array as $i => $element){
          if($element[$key] == $value){
            $temp[] = $element;
            unset($array[$i]);
          }      
       }
       return $temp;
    }
    

    Here we are saving memory and time (well, maybe not time… benchmark to see) by not making a copy of $array. Simply delete the element after you move it to the new array.

    Again, I don’t claim this to be the best way of doing it… just a way to optimize what you’ve already done.

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

Sidebar

Related Questions

I have a situation where I need to display ItemID and ItemName from Items
I have a situation like, I have two tables having same structure. I have
I have a situation where I can choose to implement a collection of string
I have a situation with my app. Suppose I have 6 users, each user
I have two datasets listing the average voltage outputs of two assemblies of neural
TFS 2010, VS 2010 We have a situation in TFS where a developer has
So my current project is mostly in Python, but I'm looking to rewrite the
While browsing the code of an erlang application, I came across an interesting design
Defined this way, we can do neither ++x++ nor ++x-- . But on the

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.