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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:33:44+00:00 2026-05-31T11:33:44+00:00

I have a PHP array that I’d like to duplicate but only copy elements

  • 0

I have a PHP array that I’d like to duplicate but only copy elements from the array whose keys appear in another array.

Here are my arrays:

$data[123] = 'aaa';
$data[423] = 'bbb';
$data[543] = 'ccc';
$data[231] = 'ddd';
$data[642] = 'eee';
$data[643] = 'fff';
$data[712] = 'ggg';
$data[777] = 'hhh';

$keys_to_copy[] = '123';
$keys_to_copy[] = '231';
$keys_to_copy[] = '643';
$keys_to_copy[] = '712';
$keys_to_copy[] = '777';

$copied_data[123] = 'aaa';
$copied_data[231] = 'ddd';
$copied_data[643] = 'fff';
$copied_data[712] = 'ggg';
$copied_data[777] = 'hhh';

I could just loop through the data array like this:

foreach ($data as $key => $value) {
  if ( in_array($key, $keys_to_copy)) {
    $copied_data[$key] = $value;
  }
}

But this will be happening inside a loop which is retrieving data from a MySQL result set. So it would be a loop nested within a MySQL data loop.
I normally try and avoid nested loops unless there’s no way of using PHP’s built-in array functions to get the result I’m looking for.
But I’m also weary of having a nested loop within a MySQL data loop, I don’t want to keep MySQL hanging around.

I’m probably worrying about nested loop performance unnecessarily as I’ll never be doing this for more than a couple of hundred rows of data and maybe 10 keys.

But I’d like to know if there’s a way of doing this with built-in PHP functions.
I had a look at array_intesect_key() but that doesn’t quite do it, because my $keys_to_copy array has my desired keys as array values rather than keys.

Anyone got any ideas?

Cheers, B

  • 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-31T11:33:46+00:00Added an answer on May 31, 2026 at 11:33 am

    I worked it out – I almost had it above.I thought I’d post the answer anyway for completeness. Hope this helps someone out!

    array_intersect_key($data, array_flip($keys_to_copy))

    Use array_flip() to switch $keys_to_copy so it can be used within array_intersect_keys()

    I’ll run some tests to compare performance between the manual loop above, to this answer. I would expect the built-in functions to be faster but they might be pretty equal. I know arrays are heavily optimised so I’m sure it will be close.

    EDIT:
    I have run some benchmarks using PHP CLI to compare the foreach() code in my question with the code in my answer above. The results are quite astounding.
    Here’s the code I used to benchmark, which I think is valid:

    <?php
    ini_set('max_execution_time', 0);//NOT NEEDED FOR CLI
    
    // BUILD RANDOM DATA ARRAY
    $data = array();
    while ( count($data) <= 200000) {
        $data[rand(0, 500000)] = rand(0, 500000);
    }
    $keys_to_copy = array_rand($data, 100000);
    
    // FOREACH
    $timer_start = microtime(TRUE);
    foreach ($data as $key => $value) {
        if ( in_array($key, $keys_to_copy)) {
            $copied_data[$key] = $value;
        }
    }
    echo 'foreach: '.(microtime(TRUE) - $timer_start)."s\r\n";
    
    // BUILT-IN ARRAY FUNCTIONS
    $timer_start = microtime(TRUE);
    $copied_data = array_intersect_key($data, array_flip($keys_to_copy));
    echo 'built-in: '.(microtime(TRUE) - $timer_start)."s\r\n";
    ?>

    And the results…
    foreach: 662.217s
    array_intersect_key: 0.099s

    So it’s much faster over loads of array elements to use the PHP array functions rather than foreach. I thought it would be faster but not by that much!

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

Sidebar

Related Questions

I have my Zend_Navigation loaded from a PHP array (but that's irrelevant...) and I'm
I have an PHP array parsed from a JSON string that looks like this:
In PHP I have an array that looks like this: $array[0]['width'] = '100'; $array[0]['height']
PROBLEM I have a nested PHP array that I need to populate from flat
I have a PHP array that looks like this: Array ( [0] => Array
I have the following php array that gets all values from a form full
I have an PHP array that looks something like this: Index Key Value [0]
I have a simple PHP Array called $categories that looks like this: Array (
I have a php array that gets returned from a database, the array contains
I have a PHP array that looks like this: [3] => Hello [7] =>

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.