New array from two other arrays with one common key. Any optimization tips?

zcyilw5tur

New Member
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:\[code\]Array( [0] => Array ( [customerid] => 1234 [name] => John Doe => [email protected] ) [1] => Array ( [customerid] => 4321 [name] => Jane Smith [email] => [email protected] ) (etc...))\[/code\]Orders array:\[code\]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...))\[/code\]Final array:\[code\]Array( [1234] => Array ( [name] => John Doe [email] => [email protected] [orders] => Array ( [0] => Array ( [amount] => 100.00 [date] => 2012-05-11 ) ) ) [4321] => Array ( [name] => Jane Smith [email] => [email protected] [orders] => Array ( [0] => Array ( [amount] => 200.00 [date] => 2012-03-01 ) [1] => Array ( [amount] => 500.00 [date] => 2012-02-22 ) ) ) (etc...))\[/code\]So... This is the PHP code that I thought of:\[code\]$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'] );}\[/code\]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...
 
Back
Top