php
  • arrays
  • sorting
  • 2011-09-30 38 views 1 likes 
    1

    我希望任何人都可以幫我解決一個小問題。得到一個清晰的頭腦後,我仍然堅持,並找不到解決方案。我想這顯然很簡單。(PHP)特定的數組排序

    我堅持一個數組結構,並需要對它們進行排序。下面是該陣列的一個小例子:

    $brand["BRAND_1"]["name"] = 'Brand Name 1'; 
    $brand["BRAND_1"]["list"]['SUB_BRAND_1']['name'] = 'Headline Subbrand 1'; 
    $brand["BRAND_1"]["list"]['SUB_BRAND_1']['text'] = 'text for Subbrand 1'; 
    $brand["BRAND_1"]["list"]['SUB_BRAND_2']['name'] = 'Headline Subbrand 2'; 
    $brand["BRAND_1"]["list"]['SUB_BRAND_2']['text'] = 'text for Subbrand 2'; 
    
    $brand["BRAND_2"]["name"] = 'Brand Name 2'; 
    $brand["BRAND_2"]["list"]['SUB_BRAND_1']['name'] = 'Headline Subbrand 1'; 
    $brand["BRAND_2"]["list"]['SUB_BRAND_1']['text'] = 'text for Subbrand 1'; 
    

    可能有BRAND_N正量和n SUB_BRAND_N量。

    我有一個訂單ID,其stucks一個靈活的訂單:

    0 = out as in (dont worry about this, catch it at an earlier stage) 
    1 = BRAND_1.SUB_BRAND_1 
    2 = BRAND_1.SUB_BRAND_2 
    3 = BRAND_2.SUB_BRAND_1 
    

    此被延長,如果有更多的這四種情況以及本身可能更改順序。所以我必須使用這些ID。

    順序應改變陣列中的位置和ID的值推到頂部陣列中,例如:

    訂單ID = 2:

    $brand["BRAND_1"]["name"] = 'Brand Name 1'; 
    $brand["BRAND_1"]["list"]['SUB_BRAND_2']['name'] = 'Headline Subbrand 2'; 
    $brand["BRAND_1"]["list"]['SUB_BRAND_2']['text'] = 'text for Subbrand 2'; 
    $brand["BRAND_1"]["list"]['SUB_BRAND_1']['name'] = 'Headline Subbrand 1'; 
    $brand["BRAND_1"]["list"]['SUB_BRAND_1']['text'] = 'text for Subbrand 1'; 
    
    $brand["BRAND_2"]["name"] = 'Brand Name 2'; 
    $brand["BRAND_2"]["list"]['SUB_BRAND_1']['name'] = 'Headline Subbrand 1'; 
    $brand["BRAND_2"]["list"]['SUB_BRAND_1']['text'] = 'text for Subbrand 1'; 
    

    順序ID = 3:

    $brand["BRAND_2"]["name"] = 'Brand Name 2'; 
    $brand["BRAND_2"]["list"]['SUB_BRAND_1']['name'] = 'Headline Subbrand 1'; 
    $brand["BRAND_2"]["list"]['SUB_BRAND_1']['text'] = 'text for Subbrand 1'; 
    
    $brand["BRAND_1"]["name"] = 'Brand Name 1'; 
    $brand["BRAND_1"]["list"]['SUB_BRAND_1']['name'] = 'Headline Subbrand 1'; 
    $brand["BRAND_1"]["list"]['SUB_BRAND_1']['text'] = 'text for Subbrand 1'; 
    $brand["BRAND_1"]["list"]['SUB_BRAND_2']['name'] = 'Headline Subbrand 2'; 
    $brand["BRAND_1"]["list"]['SUB_BRAND_2']['text'] = 'text for Subbrand 2'; 
    

    我至今嘗試過這樣的:

    <?php 
    
    $order[1] = 'BRAND_1::SUB_BRAND_1'; 
    $order[2] = 'BRAND_1::SUB_BRAND_2'; 
    $order[3] = 'BRAND_2::SUB_BRAND_1'; 
    
    // for testing 
    $orderId = 2; 
    
    // get the brand and sub_brand 
    $part = explode("::", $order[$orderId]); 
    
    // set the new brand list 
    $newBrand[$part[0]]['name'] = $brand[$part[0]]['name']; 
    $newBrand[$part[0]]['list'][$part[1]] = $brand[$part[0]]['list'][$part[1]]; 
    
    // unset the brand which should be first of the main brand array 
    unset($brand[$part[0]]['list'][$part[1]]); 
    
    // if there was only one list unset the whole brand part 
    if(count($brand[$part[0]]['list']) < 1) { 
        unset($brand[$part[0]]); 
    } 
    

    現在我有兩個數組:

    $品牌包括整個品牌,除了這一個應該是第一

    $ newBrand只包括該品牌這應該是在頂部

    現在我只需要添加$品牌$ newBrand但有我的問題:) 嘗試了很多不同的方式重建數組推動內容,替換或合併... b我總是用圈子跑。

    任何其他的想法,更短的代碼,更好的方法..?

    我寫的整個代碼爲codepad.org更好的測試: Codepad example

    讚賞任何幫助或建議。

    乾杯!

    編輯:

    很抱歉的誤解:

    順序是代碼以外的規範。目標是根據orderId將一個元素設置爲$ brand數組中的頂部。 $ orderId將通過POST,GET或類調用傳遞。

    $ order數組只是一個數組,它可以幫助我在代碼中訪問規範。

    所以$ orderId匹配$ order數組的一個元素,並返回這個元素,它應該位於$ brand數組的頂部。由於沒有數字鍵,我決定使用「brand :: sub_brand」語法來訪問這兩個深度級別。

    希望這解釋它更好地litte。

    感謝

    +0

    訂單ID只確定第一要素,其他元素將按照'order IDs'數組中的順序排列,對吧? – galymzhan

    +0

    你能澄清一下:你必須從一開始就使用數組結構?你是說這種情況可以改變嗎?如果是這樣,你在哪裏得到你的排序說明? – Shad

    +0

    *更新,並希望這回答您的問題 – Talisin

    回答

    1

    這裏是一個可能的解決方案(測試here):

    <?php 
    
    function getOrderingRules() 
    { 
        return array(
         1 => 'BRAND_1.SUB_BRAND_1', 
         2 => 'BRAND_1.SUB_BRAND_2', 
         3 => 'BRAND_2.SUB_BRAND_1', 
        ); 
    } 
    
    function getOrderedBrands($brands, $orderId) 
    { 
        $rules = getOrderingRules(); 
        if (!isset($rules[$orderId])) { 
         throw new RuntimeException("Rule for order id '$orderId' is not specified"); 
        } 
    
        $result = array(); 
    
        // Push the first element 
        list($key, $subkey) = explode('.', $rules[$orderId]); 
        $result[$key] = array(
         'name' => $brands[$key]['name'], 
         'list' => array(
          $subkey => $brands[$key]['list'][$subkey], 
         ), 
        ); 
    
        // Push remaining elements in the order they appear in $rules 
        foreach ($rules as $oid => $rule) { 
         // Skip order id of the first element 
         if ($oid == $orderId) { 
          continue; 
         } 
         list($key, $subkey) = explode('.', $rules[$oid]); 
         if (!isset($result[$key])) { 
          $result[$key] = array(
           'name' => $brands[$key]['name'], 
           'list' => array(), 
          ); 
         } 
         $result[$key]['list'][$subkey] = $brands[$key]['list'][$subkey]; 
        } 
    
        return $result; 
    } 
    
    // Loading all brands (could be external source, like database) 
    $brand["BRAND_1"]["name"] = 'Brand Name 1'; 
    $brand["BRAND_1"]["list"]['SUB_BRAND_1']['name'] = 'Headline Subbrand 1'; 
    $brand["BRAND_1"]["list"]['SUB_BRAND_1']['text'] = 'text for Subbrand 1'; 
    $brand["BRAND_1"]["list"]['SUB_BRAND_2']['name'] = 'Headline Subbrand 2'; 
    $brand["BRAND_1"]["list"]['SUB_BRAND_2']['text'] = 'text for Subbrand 2'; 
    
    $brand["BRAND_2"]["name"] = 'Brand Name 2'; 
    $brand["BRAND_2"]["list"]['SUB_BRAND_1']['name'] = 'Headline Subbrand 1'; 
    $brand["BRAND_2"]["list"]['SUB_BRAND_1']['text'] = 'text for Subbrand 1'; 
    
    // Sort and output 
    print_r(getOrderedBrands($brand, 1)); 
    print_r(getOrderedBrands($brand, 2)); 
    print_r(getOrderedBrands($brand, 3)); 
    

    你應該知道,這種陣列結構($brands),您將無法設置排序規則一樣這樣的:

    1 = BRAND_1.SUB_BRAND_1 
    2 = BRAND_2.SUB_BRAND_1 
    3 = BRAND_1.SUB_BRAND_2 
    

    ,因爲一旦你遇見元素通過BRAND_1鍵,您必須遍歷所有的它的子品牌。如果你沒有這樣的規則,一切都很好。否則,你必須存儲有序陣列結構是這樣的(因爲實際上要排序的子品牌,而不是品牌):

    $subBrands = array(
        array(
         'name' => 'Headline Subbrand 1', 
         'text' => 'Text for it', 
         'parent' => 'BRAND_1', 
         'key' => 'SUB_BRAND_1', 
        ), 
        array(
         'name' => 'Headline Subbrand 2', 
         'text' => 'Text for it', 
         'parent' => 'BRAND_1', 
         'key' => 'SUB_BRAND_2', 
        ), 
    ); 
    $parentBrands = array(
        'BRAND_1' => 'Brand Name 1', 
        'BRAND_2' => 'Brand Name 2', 
    ); 
    

    然後你就可以排序$subBrands和遍歷它

    +0

    這真棒。這不會是一個混合子品牌的規則。所以你的解決方案完美。我也想出了我的失敗之處。多謝,夥計! – Talisin

    相關問題