2

Ok guys, I have an php associative array in the following format .

groupdata: Array
(
    [0] => Array
        (
            [id] => 3324
            [name] => Testme2
            [creationDate] => 31-MAR-14 04.18.29.618000 PM

        )

    [1] => Array
        (
            [id] => 3325
            [name] => paris
            [creationDate] => 31-MAR-14 06.43.28.291000 PM
        )

    [2] => Array
        (
            [id] => 3236
            [name] => GL_HF
            [creationDate] => 29-MAY-14 12.42.01.444000 PM
        )

    [3] => Array
        (
            [id] => 3251
            [name] => attempting
            [creationDate] => 24-FEB-14 03.47.54.732000 PM
        )

    [4] => Array
        (
            [id] => 3272
            [name] => Testme
            [creationDate] => 03-MAR-14 11.24.58.671000 AM
        )
)

Now I want to sort this array based on their names. How can i achieve this ?

I have tried to use ksort/asort methods but it is not wroking.

The desired array is :

groupdata: Array
(
 [0] => Array
        (
            [id] => 3251
            [name] => attempting
            [creationDate] => 24-FEB-14 03.47.54.732000 PM
        )
[1] => Array
        (
            [id] => 3236
            [name] => GL_HF
            [creationDate] => 29-MAY-14 12.42.01.444000 PM
        )
[2] => Array
        (
            [id] => 3325
            [name] => paris
            [creationDate] => 31-MAR-14 06.43.28.291000 PM
        )
[3] => Array
        (
            [id] => 3272
            [name] => Testme
            [creationDate] => 03-MAR-14 11.24.58.671000 AM
        )
[4] => Array
        (
            [id] => 3324
            [name] => Testme2
            [creationDate] => 31-MAR-14 04.18.29.618000 PM

        )
)
3
  • 1
    use usort Commented May 30, 2014 at 6:22
  • Does the data come from a database? Commented May 30, 2014 at 6:23
  • 1
    I suggest you to change the title as it is misleading Commented May 30, 2014 at 6:24

3 Answers 3

1
$price = array();
foreach ($groupdata as $key => $row)
{
    $price[$key] = $row['name'];
}
array_multisort($price, SORT_DESC, $groupdata);

Read more array_multisort()

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

3 Comments

your solution is correct, but a little bit overkill
Thanks a lott !! I had used SORT_ASC as i wanted them in ascending order :) This worked out as expected and desired ;)
array_multisort sorts data on the basis of case sensitive alphabets. An example on the application of this function ouputs following : 1. 1750test 2. 1st Group 3. AL 4. ActiveTeach-2 5. Created today 30_5 6. Not a empty group 7. Not a empty group 8. Qwerty 9. Test210 10. my ebook Notic the 10th entry i.e my ebook at the last which was not as required. So I have used usort function as suggested below by Kevin !
0

Alternatively, you could use usort() in conjunction to strtolower(). Consider this example:

$groupdata = array(array('id' => 3324,'name' => 'Testme2','creationDate' => '31-MAR-14 04.18.29.618000 PM'),array('id' => 3325,'name' => 'paris','creationDate' => '31-MAR-14 04.18.29.618000 PM'),array('id' => 3236,'name' => 'GL_HF','creationDate' => '31-MAR-14 04.18.29.618000 PM'),array('id' => 3251,'name' => 'attempting','creationDate' => '31-MAR-14 04.18.29.618000 PM'),array('id' => 3272,'name' => 'Testme','creationDate' => '31-MAR-14 04.18.29.618000 PM'),);

function sorter($a, $b) {
    if ($a['name'] == $b['name']){
        return 0;
    }
    return strtolower($a['name']) < strtolower($b['name']) ? -1 : 1;
}

usort($groupdata, 'sorter');

Sample Output

1 Comment

Thanks KEvin . This is what was expected :)
0

Use usort, so you can define your own way to compare elements:

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


usort($your_array, "cmp");

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.