[ create a new paste ] login | about

Link: http://codepad.org/Mre9aeD9    [ raw code | output | fork ]

PHP, pasted on Sep 30:
<?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));


Output:
Array
(
    [BRAND_1] => Array
        (
            [name] => Brand Name 1
            [list] => Array
                (
                    [SUB_BRAND_1] => Array
                        (
                            [name] => Headline Subbrand 1
                            [text] => text for Subbrand 1
                        )

                    [SUB_BRAND_2] => Array
                        (
                            [name] => Headline Subbrand 2
                            [text] => text for Subbrand 2
                        )

                )

        )

    [BRAND_2] => Array
        (
            [name] => Brand Name 2
            [list] => Array
                (
                    [SUB_BRAND_1] => Array
                        (
                            [name] => Headline Subbrand 1
                            [text] => text for Subbrand 1
                        )

                )

        )

)
Array
(
    [BRAND_1] => Array
        (
            [name] => Brand Name 1
            [list] => Array
                (
                    [SUB_BRAND_2] => Array
                        (
                            [name] => Headline Subbrand 2
                            [text] => text for Subbrand 2
                        )

                    [SUB_BRAND_1] => Array
                        (
                            [name] => Headline Subbrand 1
                            [text] => text for Subbrand 1
                        )

                )

        )

    [BRAND_2] => Array
        (
            [name] => Brand Name 2
            [list] => Array
                (
                    [SUB_BRAND_1] => Array
                        (
                            [name] => Headline Subbrand 1
                            [text] => text for Subbrand 1
                        )

                )

        )

)
Array
(
    [BRAND_2] => Array
        (
            [name] => Brand Name 2
            [list] => Array
                (
                    [SUB_BRAND_1] => Array
                        (
                            [name] => Headline Subbrand 1
                            [text] => text for Subbrand 1
                        )

                )

        )

    [BRAND_1] => Array
        (
            [name] => Brand Name 1
            [list] => Array
                (
                    [SUB_BRAND_1] => Array
                        (
                            [name] => Headline Subbrand 1
                            [text] => text for Subbrand 1
                        )

                    [SUB_BRAND_2] => Array
                        (
                            [name] => Headline Subbrand 2
                            [text] => text for Subbrand 2
                        )

                )

        )

)


Create a new paste based on this one


Comments: