我希望任何人都可以幫我解決一個小問題。得到一個清晰的頭腦後,我仍然堅持,並找不到解決方案。我想這顯然很簡單。(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。
感謝
訂單ID只確定第一要素,其他元素將按照'order IDs'數組中的順序排列,對吧? – galymzhan
你能澄清一下:你必須從一開始就使用數組結構?你是說這種情況可以改變嗎?如果是這樣,你在哪裏得到你的排序說明? – Shad
*更新,並希望這回答您的問題 – Talisin