2

I have an array as below..

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [name] => Henry
                )
        )
    [1] => Array
        (
            [0] => Array
                (
                    [name] => jack
                )
        )
    [2] => Array
        (
            [0] => Array
                (
                    [name] => Albert
                )
        )
    [3] => Array
        (
            [0] => Array
                (
                    [name] => bunny
                )
        )

I need to sort this array by name in asc & desc order of case insensitive. Please help me..

0

3 Answers 3

2

I wrote a php function a few years ago which does such a thing.

function subval_sort($a, $subkey, $reverse = false)
{
    if (!$a)
    {
        return array();
    }

    $b = array();

    foreach ($a as $k => $v)
    {
        $b[$k] = strtolower($v[$subkey]);
    }

    if ($reverse)
    {
        arsort($b);
    }
    else
    {
        asort($b);  
    }

    $c = array();

    foreach ($b as $key => $val)
    {
        $c[] = $a[$key];
    }

    return $c;
}

Use it like subval_sort($array, 'name')

Sign up to request clarification or add additional context in comments.

Comments

0

sorting function :

Use my custom function to achieve your solution it is working

    function multisort (&$array, $key) {
$valsort=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
    $valsort[$ii]=$va[$key];
}
asort($valsort);
foreach ($valsort as $ii => $va) {
    $ret[$ii]=$array[$ii];
}
$array=$ret;
}

multisort($multiarr,"order");

Hope this will sure help you.

Comments

0
$array = array(
  array('Henry'),
  array('Michael'),
  array('Steve'),
  array('Daniel'),
  array('Albert'),
);


// Comparison function
function cmp($a, $b) {
    if ($a[0] == $b[0]) {
        return 0;
    }
    return ($a[0] < $b[0]) ? -1 : 1;
}

// Array to be sorted
print_r($array);

// Sort and print the resulting array
uasort($array, 'cmp');
print_r($array);

Output:

Array
(
    [0] => Array
        (
            [0] => Henry
        )

    [1] => Array
        (
            [0] => Michael
        )

    [2] => Array
        (
            [0] => Steve
        )

    [3] => Array
        (
            [0] => Daniel
        )

    [4] => Array
        (
            [0] => Albert
        )

)
Array
(
    [4] => Array
        (
            [0] => Albert
        )

    [3] => Array
        (
            [0] => Daniel
        )

    [0] => Array
        (
            [0] => Henry
        )

    [1] => Array
        (
            [0] => Michael
        )

    [2] => Array
        (
            [0] => Steve
        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.