php
  • arrays
  • sorting
  • 2011-09-30 45 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

    相关问题