Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6658427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:54:46+00:00 2026-05-26T01:54:46+00:00

I hope anyone can help me to fix a little problem. After getting a

  • 0

I hope anyone can help me to fix a little problem. After getting a clear mind I still stuck and can’t find a solution. I guess it is something obviously simple.

I stuck to an array structure and need to sort them. Here is a small example of the array:

$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';

There could be n amount of BRAND_N and n amount of SUB_BRAND_N.

I have an order ID which stucks to a flexible order:

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

This gets extended if there are more as these four cases and the order itself could change. So I have to work with the IDs.

The order should change the position in the array and push the value of the Id to the top in the array, for example:

order 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';

order 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';

I have tried this so far:

<?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]] );
}

Now I have two arrays:

$brand includes the whole brands except this one which should be the first

$newBrand includes only this brand which should be at the top

Now I just need to add $brand to $newBrand but there is my problem 🙂
Tried a lot of different ways from rebuilding the array to push the content, replace or merge… but I always run in circles.

Any other idea, shorter code, better approach..?

I wrote the whole code into codepad.org for better testing:
Codepad example

Any help or suggestion appreciated.

Cheers!

EDIT:

Sorry for the misunderstanding:

The order is a specification outside the code. The aim is to set one element depending on the orderId to the top in the $brand array. The $orderId will get passed by POST, GET or a class call.

The $order array is just an array which helps me to make the specification accessible in the code.

So the $orderId match one element of the $order array and will return this element which should be on the top of the $brand array. Due to the fact that there is no numeric keys I decide to use the “brand::sub_brand” syntax to access on both depth level.

Hope this explains it a litte better.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:54:47+00:00Added an answer on May 26, 2026 at 1:54 am

    Here is one possible solution (test it 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));
    

    You should be aware that with this array structure ($brands) you won’t be able to set ordering rules like this:

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

    because once you met element keyed by BRAND_1, you have to iterate over all of it’s sub-brands. If you don’t have such rules, everything is fine. Otherwise you have to store sorted array structured like this (because actually you are sorting sub-brands, not brands):

    $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',
    );
    

    Then you can sort $subBrands and iterate over it

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am stuck and in need of a hand. Hope someone can help? Anyone
I hope someone can help with solution to this problem? Currently my ASP.Net MVC
Hope to get solution to this problem. I have been stuck on it since
Really hope someone can help me as I'm a bit stuck :S I have
Hi guys I hope anyone can help me. I'm running a simple program in
I hope anyone from the large community here can help me write the simplest
I hope anyone can help. I have to develop up against this third party
I'm having some problems with my stored procedure. Hope anyone can help me figure
I'm new here and I hope anyonte can help me. I have WCF Service
Hope anyone can shed light on this so I can use pens with dash

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.