0

I am having the array of color attribute options...

I would like to sort the array based on 'store0' key...

I have used multisort and usort but its not working

[821] => Array
    (
        [checked] => 
        [intype] => radio
        [id] => 969
        [sort_order] => 513
        [store1] => Yellow
        [defaultswatch1] => 
        [swatch1] => 
        [store0] => Yellow - KID
        [defaultswatch0] => 
        [swatch0] => 
    )

[822] => Array
    (
        [checked] => 
        [intype] => radio
        [id] => 207
        [sort_order] => 309
        [store1] => Yellow Beige - Chroma 1 ½
        [defaultswatch1] => 
        [swatch1] => 
        [store0] => Yellow Beige - Chroma 1 ½
        [defaultswatch0] => 
        [swatch0] => 
    )

[823] => Array
    (
        [checked] => 
        [intype] => radio
        [id] => 857
        [sort_order] => 403
        [store1] => Yellow
        [defaultswatch1] => 
        [swatch1] => 
        [store0] => Yellow-RAIN HAT BOB
        [defaultswatch0] => 
        [swatch0] => 
    )

[824] => Array
    (
        [checked] => 
        [intype] => radio
        [id] => 860
        [sort_order] => 406
        [store1] => Yellow
        [defaultswatch1] => 
        [swatch1] => 
        [store0] => Yellow-WOMEN
        [defaultswatch0] => 
        [swatch0] => 
    )

[825] => Array
    (
        [checked] => 
        [intype] => radio
        [id] => 851
        [sort_order] => 400
        [store1] => Yellow
        [defaultswatch1] => 
        [swatch1] => 
        [store0] => Yellow-WOMEN1
        [defaultswatch0] => 
        [swatch0] => 
    )

[826] => Array
    (
        [checked] => 
        [intype] => radio
        [id] => 29
        [sort_order] => 310
        [store1] => Zebra
        [defaultswatch1] => 
        [swatch1] => 
        [store0] => Zebra
        [defaultswatch0] => 
        [swatch0] => 
    )

[827] => Array
    (
        [checked] => 
        [intype] => radio
        [id] => 1383
        [sort_order] => 830
        [store1] => abstract
        [defaultswatch1] => 
        [swatch1] => 
        [store0] => abstract
        [defaultswatch0] => 
        [swatch0] => 
    )

[828] => Array
    (
        [checked] => 
        [intype] => radio
        [id] => 1382
        [sort_order] => 829
        [store1] => caol color
        [defaultswatch1] => 
        [swatch1] => 
        [store0] => caol color
        [defaultswatch0] => 
        [swatch0] => 
    )

[829] => Array
    (
        [checked] => 
        [intype] => radio
        [id] => 1381
        [sort_order] => 829
        [store1] => cool color
        [defaultswatch1] => 
        [swatch1] => 
        [store0] => cool color
        [defaultswatch0] => 
        [swatch0] => 
    )
  • I know its a simple question .. but something went wrong... – 1990rk4 May 30 at 6:28
1

Here is the example, you can use this


function asc_sort (&$array, $key) {
    $sorter = array();
    $ret = array();
    reset($array);
    foreach ($array as $ii => $va) {
        $sorter[$ii] = $va[$key];
    }
    asort($sorter);
    foreach ($sorter as $ii => $va) {
        $ret[$ii] = $array[$ii];
    }
    $array = $ret;
}
asc_sort($a_array, "store0");

1
$arr = your array from above;

usort(
    $arr, 
    function($a, $b) {
        return strcmp($a['store0'], $b['store0']);
    }
);

the usort function receives the first parameter by reference. So after the code above you can just iterate through $arr. It should be ordered by the store0 field.

0
$array = your_array;

function compareOrder($x, $y)
{
  return $x['store0'] - $y['store0'];
}
usort($array, 'compareOrder');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.