我希望任何人都可以帮我解决一个小问题。得到一个清晰的头脑后,我仍然坚持,并找不到解决方案。我想这显然很简单。(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