Search code examples
phparray-multisort

array_multisort sort by order of appearance of assisting array


I'd like your help with this tough situation I'm in. Suppose the following array $_xm_anal that holds the sizes/availability info of a product:

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
        )

)

The PHP code that translates the above array into the appropriate size options is the one below:

                        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>
                            <? } ?>
                        <? } ?>

And the final outcome looks like this:

enter image description here

The problem started when they added sizes in an unsorted manner, ie size 38 corresponds to key SIZE24, so $_xm_anal['availability']['BLK'] needs to be sorted in the appropriate order according to its keys, which can be determined by $_xm_anal['text_size']

So I wrote the code below to first create an array of the correct order of $_xm_anal['availability']['BLK'] keys:

    $tmp = $_xm_anal['text_size'];
    asort($tmp);
    $tmp2 = array_keys($tmp);

This produces the array $tmp2 that equals to:

Array
(
    [0] => SIZE24
    [1] => SIZE4
    [2] => SIZE8
    [3] => SIZE10
    [4] => SIZE14
    [5] => SIZE16
    [6] => SIZE19
)

So the only problem now is that I can't find the correct flag to use in the appropriate array_multisort to sort out the initial $_xm_anal array based on that $tmp2 array...

array_multisort($tmp2, SORT_NATURAL, $_xm_anal["availability"][$Prod["color_code"]]);

What is the correct flag to use to actually use the $tmp2 in the order its keys are met?


Solution

  • OK, I was able to sort it a bit indirectly/less elegant, but I couldn't think of any better solution... So I did this:

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

    If anyone can think of a more elegant solution, please feel free to post it! TIA