3

所以这就是我正在做的事情,我有一个包含客户的数组和一个包含订单的数组,所以我正在从这两个数据中创建一个新数组......这是一些代码:

客户阵列:

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']
  );
}

终于,我们来到了这篇文章的主题!你们中的任何人有任何以任何方式优化它的技巧吗?或者一开始就在正确的轨道上...不过那也很好...无论如何,即使我选择不遵循它们,任何提示都将受到赞赏...在此先感谢...

4

2 回答 2

1

正如我在评论中所说,它看起来不错。不过,您可以做的一件事来缩短您的代码,即在创建新数组时不要再次提及所有字段,如果您最终有很多字段,这可以为您节省相当多的打字时间。

例如,而不是:

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_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);

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

于 2012-05-11T13:05:19.753 回答
0

看起来对我来说是正确的,虽然我不是 PHP 专家。不能优化更多。另外,我会小心过早的优化。在你四处尝试加快速度之前让你的代码完全运行——它会在未来为你节省一个痛苦的世界。

于 2012-05-11T13:01:52.020 回答