Php 使用一个公共密钥从其他两个数组中创建新数组。有什么优化建议吗?

Php 使用一个公共密钥从其他两个数组中创建新数组。有什么优化建议吗?,php,arrays,optimization,multidimensional-array,php-5.2,Php,Arrays,Optimization,Multidimensional Array,Php 5.2,这就是我正在做的,我有一个客户阵列和一个订单阵列,所以我从这两个数据中创建了一个新阵列。。。下面是一些代码: 客户阵列: Array ( [0] => Array ( [customerid] => 1234 [name] => John Doe [email] => john@doe.com ) [1] => 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
(
    [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']
  );
}

最后,我们来到了这篇文章的主题!你们当中有没有人有任何方法来优化这一点?或者一开始就走上正轨了。。。那也太好了。。。不管怎样,任何提示都是很感激的,即使我选择不遵循它们。。。提前感谢…

看起来很适合我,尽管我不是PHP专家。再优化不过了。另外,我会小心过早的优化。在你尝试加速之前,让你的代码完全工作起来——这将在未来为你省去一片痛苦。

正如我在评论中所说,它看起来不错。但是,要缩短代码,可以做的一件事是在创建新数组时不要再提及所有字段,如果最后有很多字段,这可以节省大量的键入时间

例如,而不是:

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有一个内部引用计数机制,其中数组的副本不是在赋值时生成的,而是在以后实际修改数组时生成的(这称为写时复制)。例如:

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\u 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'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);

但我再次重申,没有必要像这样过早地进行优化。更喜欢干净易读的代码,只有在必要时才进行优化。

你所做的一切都很好。如果它有效,就使用它!在您发现速度或内存问题之前,无需进行优化。考虑到数据的格式,我可能也会这样做:)我想在一切正常后,我会实施这些优化,即使只有几秒钟的差异,从长远来看,也会很好。实际阵列有几十万个订单,大约有一万个客户。。。不要问我为什么会这样,首先,我不是最初的程序员。。。