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...);
$orders = array(blah...);
$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 ...
source
share