【问题标题】:New array from two other arrays with one common key. Any optimization tips?来自其他两个数组的新数组,具有一个公共键。有什么优化技巧吗?
【发布时间】:2012-05-20 01:54:44
【问题描述】:

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

客户数组:

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 arrays optimization multidimensional-array php-5.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);

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

【讨论】:

  • 我想我会在一切正常后实施这些优化,即使只有几秒钟的差异,从长远来看也是好的。实际的数组在几十万的订单大概一万左右……别问我为什么会这样,我不是最初的程序员……
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2021-10-10
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 2012-08-16
    • 2019-01-06
    相关资源
    最近更新 更多