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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:07:58+00:00 2026-06-09T17:07:58+00:00

Suppose we have two arrays: $a=array(‘1’=>’Apple’,’2’=>’Microsoft’, ‘3’=>’Microapple’,’4’=>’Applesoft’,’5’=>’Softapple’); $b=array(1,3); Where $b array represents the keys

  • 0

Suppose we have two arrays:

$a=array('1'=>'Apple','2'=>'Microsoft',
         '3'=>'Microapple','4'=>'Applesoft','5'=>'Softapple');
$b=array(1,3);

Where $b array represents the keys of array $a to be differentiated against.

And we expect to receive another array $c with the following values:

$c=array('2'=>'Microsoft','4'=>'Applesoft','5'=>'Softapple');

In php manual there are two functions:

array_diff($array1,$array2);    //difference of values
array_diff_key($array1,$array2);//difference of keys

But neither of the above is applicable here.

What should we do?

Edit

Thanks everyone for contribution.

I performed some benchmarks on two arrays predefined as follows:

for ($i=0; $i < 10000; $i++) {    //add 10000 values
    $a[]=mt_rand(0, 1000000); //just some random number as a value
}
for ($i=0; $i < 10000; $i++) {    //add 10000 values as keys of a
    $b[]=mt_rand(0, 1000);    
}        //randomly from 0 to 1000 (eg does not cover all the range of keys)

Each test was also taken 10000 times, the average time of Nanne‘s solution was:

0.013398

And the one of decereé:

0.014865

Which is also excellent.

…Unlike some other suggestion with in_array() but (that answer was deleted):

foreach ($a as $key => $value)
if (!in_array($key, $b)) 
$c[$key] = $value;

The above did 2 seconds on average. For the obvious reason that in_array() would have to loop through the $b to check whether the value existed. The above is an excellent example how not to do it! 🙂

  • 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-09T17:07:59+00:00Added an answer on June 9, 2026 at 5:07 pm

    I would just code it like:

    $c = $a;
    foreach ($b as $removeKey) {
        unset($c[$removeKey]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have two arrays like the following: $arr2 = array(array(first, second), array(third, fourth));
Suppose I have two arrays: val ar1 = Array[String](1, 2, 3) val ar2 =
A PHP question about arrays. Suppose we have two arrays: [first] => Array (
Suppose I have two arrays: $a1 = array(0, 1, 2); $a2 = array(3, 4,
Suppose that I have two numpy arrays of the form x = [[1,2] [2,4]
Suppose I have two arrays: a = [1, 2, 3, 4] b = [5,
Suppose I have: class A{ public $one; public $two; } and an array with
Suppose we have two arrays: a = [4,3,8,7] b = [(1,2),(5,6),(8,6),(9,0)] So what we
Suppose I have two arrays: arrayOne = [[james, 35], [michael, 28], [steven, 23], [jack,
Suppose I have two integer arrays a and b with 10 int s per

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.