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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:57:48+00:00 2026-05-11T17:57:48+00:00

So $array is an array of which all elements are references. I want to

  • 0

So $array is an array of which all elements are references.

I want to append this array to another array called $results (in a loop), but since they are references, PHP copies the references and $results is full of identical elements.

So far, the best working solution is:

$results[] = unserialize(serialize($array));

which I fear to be incredibly inefficient. Is there a better way to do this?

  • 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-11T17:57:49+00:00Added an answer on May 11, 2026 at 5:57 pm

    You can use the fact that functions dereferences results when returning, for exemple here $array_by_myclone will still have a reference to $original ($array_by_myclone[0][0] == 'foo') while $array_by_assignment will have a cloned value ($array_by_assignment[0][0] == 'bar')

    $original = 'foo';
    $array_of_reference = array(&$original);
    
    function myclone($value)
    {
      return $value;
    }
    
    $array_by_myclone = array();
    $array_by_myclone[] = array_map('myclone', $array_of_reference);
    
    $array_by_assignment = array();
    $array_by_assignment[] = $array_of_reference;
    
    $original = 'bar';
    
    var_dump($array_by_myclone[0][0]); // foo, values were cloned                                                                                                                                   
    var_dump($array_by_assignment[0][0]); // bar, still a reference                     
    

    EDIT: I wanted to check if the comment saying unserialize(serialize()) was faster was right so I did the test using php 5.5, and it turns out this is wrong: using the serialization method is slower even with a small dataset, and the more data you have the slower it gets.

    lepidosteus@server:~$ php -v
    PHP 5.5.1-1~dotdeb.1 (cli) (built: Aug  3 2013 22:19:30) 
    Copyright (c) 1997-2013 The PHP Group
    Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
        with Zend OPcache v7.0.2-dev, Copyright (c) 1999-2013, by Zend Technologies
    lepidosteus@server:~$ php reference.php 1
    myclone:   0.000010 seconds
    serialize: 0.000012 seconds
    lepidosteus@server:~$ php reference.php 1000000
    myclone:   0.398540 seconds
    serialize: 0.706631 seconds
    

    Code used:

    <?php
    $iterations = 1000000;
    if (isset($argv[1]) && is_numeric($argv[1])) {
      $iterations = max(1, (int)$argv[1]);
    }
    
    $items = array();
    for ($i = 0; $i < $iterations; $i++) {
      $items[] = 'item number '.$i;
    }
    
    $array_of_refs = array();
    foreach ($items as $k => $v) {
      $array_of_refs[] = &$items[$k];
    }
    
    function myclone($value)
    {
      return $value;
    }
    
    $start = microtime(true);
    
    $copy = array_map('myclone', $array_of_refs);
    
    $time = microtime(true) - $start;
    
    printf("%-10s %2.6f seconds\n", 'myclone:', $time);
    
    $start = microtime(true);
    
    $copy = unserialize(serialize($array_of_refs));
    
    $time = microtime(true) - $start;
    
    printf("%-10s %2.6f seconds\n", 'serialize:', $time);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 165k
  • Answers 165k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Categories extend the original class, but they don't subclass it,… May 12, 2026 at 12:54 pm
  • Editorial Team
    Editorial Team added an answer Haven't tested this, but it's something like: RewriteRule \.php$ -… May 12, 2026 at 12:54 pm
  • Editorial Team
    Editorial Team added an answer "0:0:0:0:0:0:0:1" is the IPv6 loopback address as defined in RFC… May 12, 2026 at 12:54 pm

Related Questions

I am fetching an array of floats from my database but the array I
To build a bind_param dynamically, I have found this on other SO posts. call_user_func_array(array(&$stmt,
I'm working in a LAMP environment, so PHP is the language; at least i
I have a table that's generated by a normal PHP loop. What I want

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.