A new array from two other arrays with one shared key. Any optimization tips?

So here is what I am doing, I have one array with clients and one with orders, and so I am making a new array from the data in these two ... Here is the code:

Client 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...)
)

array of orders:

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 I was thinking about:

$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 are on the subject of this post! Do any of you have any tips for optimizing it? Or was he on the right track to start ... That would be good, though ... Anyway, any advice is welcome, even if I don't follow it ... Thanks in advance ...

+3
source share
2 answers

, . , , , , , .

, :

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

:

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

, , , (, , , , ).

, , . , , , . PHP , , , ( copy-on-write). :

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
}

, , $customers $orders $new_array, , :

// 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 no need to use a reference here, as PHP 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);

, . , , .

+1

, PHP. . , . , , , - .

0

All Articles