2012-05-11 24 views
3

因此,這裏是我在做什麼,我有一個數組與客戶和一個下單訂購,所以我正在做從數據的新陣在這兩個......下面是一些代碼:使用一個公用密鑰的兩個其他數組的新數組。任何優化技巧?

客戶陣:

Array 
(
    [0] => Array 
     (
      [customerid] => 1234 
      [name] => John Doe 
      [email] => [email protected] 
     ) 

    [1] => Array 
     (
      [customerid] => 4321 
      [name] => Jane Smith 
      [email] => [email protected] 
     ) 

    (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] => [email protected] 
      [orders] => Array 
       (
        [0] => Array 
         (
          [amount] => 100.00 
          [date] => 2012-05-11 
         ) 

       ) 

     ) 

    [4321] => Array 
     (
      [name] => Jane Smith 
      [email] => [email protected] 
      [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'] 
); 
} 

最後,我們在這篇文章的話題!你們有沒有任何提示,以任何方式進行優化?或者是正確的軌道上開始...這會是不錯過,但...總之,任何提示的,即使我選擇不跟隨他們讚賞...在此先感謝...

+0

你在做什麼非常好。 –

+0

如果有效,請使用它!除非您發現速度或內存問題,否則無需進行優化。 –

+0

這可能是我也會這樣做的,因爲這樣的格式數據:) –

回答

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

但我又重新迭代,真的沒有必要過早地優化這樣。選擇乾淨可讀的代碼,只在必要時進行優化。

+0

我想我會實現這些優化之後,我得到了一切工作,即使它只有幾秒鐘的差異,它會在很長時間跑。實際的陣列對於大約一萬個客戶而言是成千上萬的訂單...不要問我爲什麼首先這樣,我不是原來的程序員...... – VuoriLiikaluoma

0

看起來約對我來說,儘管我不是PHP專家。無法進行更多的優化。另外,我會小心過早的優化。讓你的代碼工作完全你去周圍試圖加快速度up--它會痛苦的世界節省您在未來之前。

相關問題