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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:41:40+00:00 2026-06-03T20:41:40+00:00

So here’s what I’m doing, I’ve got one array with customers and one with

  • 0

So here’s what I’m doing, I’ve got one array with customers and one with orders and so I’m making a new array from the data in these two… Here’s some code:

Customer Array:

Array
(
    [0] => Array
        (
            [customerid] => 1234
            [name] => John Doe
            [email] => john@doe.com
        )

    [1] => Array
        (
            [customerid] => 4321
            [name] => Jane Smith
            [email] => jane@smith.com
        )

    (etc...)
)

Orders array:

Array
(
    [0] => Array
        (
            [customerid] => 1234
            [amount] => 100.00
            [date] => 2012-05-11
        )

    [1] => Array
        (
            [customerid] => 4321
            [amount] => 200.00
            [date] => 2012-03-01
        )

    [2] => Array
        (
            [customerid] => 4321
            [amount] => 500.00
            [date] => 2012-02-22
        )

    (etc...)
)

Final array:

Array
(
    [1234] => Array
        (
            [name] => John Doe
            [email] => john@doe.com
            [orders] => Array
                (
                    [0] => Array
                        (
                            [amount] => 100.00
                            [date] => 2012-05-11
                        )

                )

        )

    [4321] => Array
        (
            [name] => Jane Smith
            [email] => jane@smith.com
            [orders] => Array
                (
                    [0] => Array
                        (
                            [amount] => 200.00
                            [date] => 2012-03-01
                        )
                    [1] => Array
                        (
                            [amount] => 500.00
                            [date] => 2012-02-22
                        )

                )

        )

    (etc...)
)

So… This is the PHP code that I thought of:

$customers = array(blah...); # See above...
$orders    = array(blah...); # See above...
$new_array = array();

foreach ($customers as $c) {
  $new_array[$c['customerid']] = array(
    'name'   => $c['name'],
    'email'  => $c['email'],
    'orders' => array()
  );
}

foreach ($orders as $o) {
  $new_array[$o['customerid']]['orders'][] = array(
    'amount' => $o['amount'],
    'date'   => $o['date']
  );
}

Finally, we’re at the topic of this post! Do any of you have any tips for optimizing this in any way? Or was right on track to begin with… That’d be nice too though… Anyhow, any tips are appreciated even if I choose to not follow them… Thanks in advance…

  • 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-03T20:41:42+00:00Added an answer on June 3, 2026 at 8:41 pm

    As I said in the comment, it looks fine. One thing you can do to shorten your code though is not to mention all the fields again when creating the new array, which can save you quite a bit of typing if you have a lot fields in the end.

    For example instead of:

    foreach ($customers as $c) {
      $new_array[$c['customerid']] = array(
        'name'   => $c['name'],
        'email'  => $c['email'],
        'orders' => array()
      );
    }
    

    You can just do:

    foreach ($customers as $c) {
      $new_array[$c['customerid']] = $c;
      $new_array[$c['customerid']]['orders'] = array();
    }
    

    There’s usually no reason to prematurely optimize code, just make it work and optimize it if required (of course having a good basis knowledge of what is good and what is bad will help you write good quality code in the first place).

    EDIT

    If you are genuinely just curious about how to make it faster, there are ways to do it. For example if you don’t need to care about the original two arrays after you’ve created the new array, you can change your code around a bit to remove all unnecessary array copies. PHP has an internal reference counting mechanism, where a copy of an array is not made upon assignment, but only when you actually modify the array later on (this is called copy-on-write). For example:

    foreach ($customers as $c) {
      $new_array[$c['customerid']] = $c; // $c is not yet actually copied here, PHP just creates an internal reference to $c
      $new_array[$c['customerid']]['orders'] = array(); // this is what copies the underlying array, as it is now modified
    }
    

    Keeping that in mind, if you don’t care that the original $customers and $orders arrays stay untouched after generating $new_array, you can simply modify the original arrays to prevent unnecessary copying:

    // iterate through the array by reference
    foreach ($customers as &$c) {
      $c['orders'] = array(); // modifies the original $customers array
      $new_array[$c['customerid']] = $c;
    }
    
    // clean up the reference, to prevent accidents later on
    unset($c);
    
    // there's no need to use a reference here, as PHP's internal refcounting mechanism will kick in
    foreach ($orders as $o) {
      $new_array[$o['customerid']]['orders'][] = $o;
    }
    
    // finally if you don't need the original arrays anymore, clean up after them
    // this also means that modifying $new_orders afterwards will not create a copy
    // of anything, as $new_orders is the only array keeping internal references
    // to the customers and orders
    unset($customers, $orders);
    

    But again I re-iterate, there’s really no need to prematurely optimize like this. Prefer clean, readable code, and only optimize if it’s necessary.

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

Sidebar

Related Questions

Here's some CSS and HTML to make a textarea below a list of data
Here is a complete example. I want to forbid using A::set from objects casted
Here is some simple code: DIR* pd = opendir(xxxx); struct dirent *cur; while (cur
here is my php code $titikPetaInti = array(); while($row = mysql_fetch_assoc($hasil2)) { $titikPetaInti[] =
Here is the problem that I am trying to solve. I have two folders
Here's a piece of code I copied from http://www.schillmania.com/content/projects/javascript-animation-1/demo/ Very simple JS animation: function
Here's what I'm doing. I want to upload multipart file via Ajax to my
Here is the code I'm using inside my AsyncTask DefaultHttpClient httpClient = new DefaultHttpClient();
Here is my query: SELECT * FROM [GeoName] WHERE ((-26.3665122100029-Lat)*(-26.3665122100029-Lat))+((27.5978928658078-Long)*(27.5978928658078-Long)) < 0.005 ORDER BY
Here is my code...I have two dimensional matrices A,B. I want to develop 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.