所以这就是我正在做的事情,我有一个包含客户的数组和一个包含订单的数组,所以我正在从这两个数据中创建一个新数组......这是一些代码:
客户阵列:
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
(
[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...)
)
最终数组:
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...)
)
所以......这是我想到的PHP代码:
$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']
);
}
终于,我们来到了这篇文章的主题!你们中的任何人有任何以任何方式优化它的技巧吗?或者一开始就在正确的轨道上...不过那也很好...无论如何,即使我选择不遵循它们,任何提示都将受到赞赏...在此先感谢...