array_multisort 按辅助数组的出现顺序排序
array_multisort sort by order of appearance of assisting array
我现在处境艰难,希望得到你的帮助。假设以下数组 $_xm_anal
包含产品的 sizes/availability 信息:
Array
(
[availability] => Array
(
[BLK] => Array
(
[SIZE4] => 2
[SIZE8] => 1
[SIZE10] => 3
[SIZE14] => 8
[SIZE16] => 4
[SIZE19] => 12
[SIZE24] => 1
)
)
[color_list] => Array
(
[BLK] => Array
(
[0] => BLK
[COLORCODE] => BLK
[1] => BLACK
[ColorLANG] => BLACK
)
)
[size_list] => Array
(
[ID] => 20
[SIZE1] => 39
[SIZE2] => 40
[SIZE3] => 40.5
[SIZE4] => 41
[SIZE5] => 41.5
[SIZE6] => 42
[SIZE7] => 42.5
[SIZE8] => 43
[SIZE9] => 43.5
[SIZE10] => 44
[SIZE11] => 44.5
[SIZE12] => 45
[SIZE13] => 45.5
[SIZE14] => 46
[SIZE15] => 46.5
[SIZE16] => 47
[SIZE17] => 47.5
[SIZE18] => 48
[SIZE19] => 48.5
[SIZE20] => 49
[SIZE21] => 36
[SIZE22] => 37
[SIZE23] => 37.5
[SIZE24] => 38
[SIZE25] => 38.5
[TitleLANG] => NO NO NO
)
[text_size] => Array
(
[SIZE4] => 41
[SIZE8] => 43
[SIZE10] => 44
[SIZE14] => 46
[SIZE16] => 47
[SIZE19] => 48.5
[SIZE24] => 38
)
)
将上述数组转换为适当大小选项的PHP代码如下:
foreach ($_xm_anal["availability"][$Prod["color_code"]] as $k => $v) { ?> // $Prod["color_code"] equals to BLK in this example
<? if ($v) {
$hasSize = 1; ?>
<li><a href="javascript:void(0)" class="system_selectSize chk_avail" data-storehouse="2" data-size="<?= $k ?>"> <?= $_xm_anal["size_list"][$k] ?></a></li>
<? } else { ?>
<li style="background:#f0eee8"><a href="javascript:void(0)" class="noqty"> <s><?= $_xm_anal["size_list"][$k] ?></s></a></li>
<? } ?>
<? } ?>
最后的结果是这样的:
当他们以 未排序 方式添加大小时,问题就开始了,即大小 38 对应于键 SIZE24,因此 $_xm_anal['availability']['BLK']
需要按照适当的顺序排序到它的键,可以通过 $_xm_anal['text_size']
确定
所以我写了下面的代码,首先创建一个 $_xm_anal['availability']['BLK']
键的正确顺序的数组:
$tmp = $_xm_anal['text_size'];
asort($tmp);
$tmp2 = array_keys($tmp);
这会生成数组 $tmp2,它等于:
Array
(
[0] => SIZE24
[1] => SIZE4
[2] => SIZE8
[3] => SIZE10
[4] => SIZE14
[5] => SIZE16
[6] => SIZE19
)
所以现在唯一的问题是我找不到正确的flag在合适的array_multisort
中使用来整理初始的$_xm_anal
基于 $tmp2
数组的数组...
array_multisort($tmp2, SORT_NATURAL, $_xm_anal["availability"][$Prod["color_code"]]);
实际使用 $tmp2
的正确标志是满足其键的顺序吗?
好的,我能够indirectly/less 优雅地对其进行排序,但我想不出任何更好的解决方案...所以我这样做了:
$tmp = $_xm_anal['text_size'];
asort($tmp);
foreach ($tmp as $kkk => $vvv) {
$tmp[$kkk]=$_xm_anal["availability"][$Prod["color_code"]][$kkk];
}
$_xm_anal["availability"][$Prod["color_code"]] = $tmp;
如果谁能想到更优雅的方案,欢迎post提出来! TIA
我现在处境艰难,希望得到你的帮助。假设以下数组 $_xm_anal
包含产品的 sizes/availability 信息:
Array
(
[availability] => Array
(
[BLK] => Array
(
[SIZE4] => 2
[SIZE8] => 1
[SIZE10] => 3
[SIZE14] => 8
[SIZE16] => 4
[SIZE19] => 12
[SIZE24] => 1
)
)
[color_list] => Array
(
[BLK] => Array
(
[0] => BLK
[COLORCODE] => BLK
[1] => BLACK
[ColorLANG] => BLACK
)
)
[size_list] => Array
(
[ID] => 20
[SIZE1] => 39
[SIZE2] => 40
[SIZE3] => 40.5
[SIZE4] => 41
[SIZE5] => 41.5
[SIZE6] => 42
[SIZE7] => 42.5
[SIZE8] => 43
[SIZE9] => 43.5
[SIZE10] => 44
[SIZE11] => 44.5
[SIZE12] => 45
[SIZE13] => 45.5
[SIZE14] => 46
[SIZE15] => 46.5
[SIZE16] => 47
[SIZE17] => 47.5
[SIZE18] => 48
[SIZE19] => 48.5
[SIZE20] => 49
[SIZE21] => 36
[SIZE22] => 37
[SIZE23] => 37.5
[SIZE24] => 38
[SIZE25] => 38.5
[TitleLANG] => NO NO NO
)
[text_size] => Array
(
[SIZE4] => 41
[SIZE8] => 43
[SIZE10] => 44
[SIZE14] => 46
[SIZE16] => 47
[SIZE19] => 48.5
[SIZE24] => 38
)
)
将上述数组转换为适当大小选项的PHP代码如下:
foreach ($_xm_anal["availability"][$Prod["color_code"]] as $k => $v) { ?> // $Prod["color_code"] equals to BLK in this example
<? if ($v) {
$hasSize = 1; ?>
<li><a href="javascript:void(0)" class="system_selectSize chk_avail" data-storehouse="2" data-size="<?= $k ?>"> <?= $_xm_anal["size_list"][$k] ?></a></li>
<? } else { ?>
<li style="background:#f0eee8"><a href="javascript:void(0)" class="noqty"> <s><?= $_xm_anal["size_list"][$k] ?></s></a></li>
<? } ?>
<? } ?>
最后的结果是这样的:
当他们以 未排序 方式添加大小时,问题就开始了,即大小 38 对应于键 SIZE24,因此 $_xm_anal['availability']['BLK']
需要按照适当的顺序排序到它的键,可以通过 $_xm_anal['text_size']
所以我写了下面的代码,首先创建一个 $_xm_anal['availability']['BLK']
键的正确顺序的数组:
$tmp = $_xm_anal['text_size'];
asort($tmp);
$tmp2 = array_keys($tmp);
这会生成数组 $tmp2,它等于:
Array
(
[0] => SIZE24
[1] => SIZE4
[2] => SIZE8
[3] => SIZE10
[4] => SIZE14
[5] => SIZE16
[6] => SIZE19
)
所以现在唯一的问题是我找不到正确的flag在合适的array_multisort
中使用来整理初始的$_xm_anal
基于 $tmp2
数组的数组...
array_multisort($tmp2, SORT_NATURAL, $_xm_anal["availability"][$Prod["color_code"]]);
实际使用 $tmp2
的正确标志是满足其键的顺序吗?
好的,我能够indirectly/less 优雅地对其进行排序,但我想不出任何更好的解决方案...所以我这样做了:
$tmp = $_xm_anal['text_size'];
asort($tmp);
foreach ($tmp as $kkk => $vvv) {
$tmp[$kkk]=$_xm_anal["availability"][$Prod["color_code"]][$kkk];
}
$_xm_anal["availability"][$Prod["color_code"]] = $tmp;
如果谁能想到更优雅的方案,欢迎post提出来! TIA