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
  • 4 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 two activities in which I need to pass an object array between
I have a situation here, I have two arrays,array1 and array2 and one tableview.
Following situation: I have an array with some constant values, which represent ranges. A
I have a situation where I have two arrays of objects. I want select
I have a quadratic matrix(two-dimensional dynamic array of pointers) and need to change rows/columns
I need a javascript solution for the following situation: I have two select drop
I have a situation where two objects of the same type have parents of
I have a rather classic UI situation - two ListBoxes named SelectedItems and AvailableItems
In this situation I have two models, Comment and Score. The relationship is defined
I have a situation where I have two entities that share a primary key

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.