---
title: "problem med att sortera en array"
type: "forum-thread"
url: "https://www.webforum.nu/amne/php/116996-problem-med-att-sortera-en-array"
topic: "PHP"
topic_url: "https://www.webforum.nu/amne/php"
author: "Qonos"
published: "2004-11-29T19:45:04.000Z"
updated: "2004-11-30T18:36:55.000Z"
replies: 5
views: 257
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/php/116996-problem-med-att-sortera-en-array"
---

# problem med att sortera en array

## #1 — Qonos, 2004-11-29T19:45Z

Hmm jag må vara trög, men trotts att jag läst manualen så lyckas jag inte med att sortera denna array efter \[type\].

ksort() gissar jag på att man ska använda, men får ändå inte till det.

```php
Array
(
    [0] => Array
        (
            [name] => typ2
            [type] => 2
        )

    [1] => Array
        (
            [name] => typ1
            [type] => 1
            [answer] => Array
                (
                    [0] => dsf
                    [1] => sdf
                    [2] => sdf
                )

        )

    [2] => Array
        (
            [name] => 32425678
            [type] => 2
        )

)
```

Permalänk: https://www.webforum.nu/p/116996

## #2 — WiZzLeR, 2004-11-29T20:40Z

```php
function mu_sort($array, $key_sort) {

   $key_sorta = explode(",", $key_sort);

   $keys = array_keys($array[0]);

     for($m=0; $m < count($key_sorta); $m++){ $nkeys[$m] = trim($key_sorta[$m]); }

   $n += count($key_sorta);

     for($i=0; $i < count($keys); $i++){

         if(!in_array($keys[$i], $key_sorta)){

             $nkeys[$n] = $keys[$i];

             $n += "1";

           }

     }

     for($u=0;$u<count($array); $u++){ 

         $arr = $array[$u];

           for($s=0; $s<count($nkeys); $s++){

               $k = $nkeys[$s];
            
                 $output[$u][$k] = $array[$u][$k];

           } 

     } 

   sort($output);

 return $output;

}

$array[0]['name'] = "typ2";
$array[0]['type'] = 2;

$array[1]['name'] = "typ1";
$array[1]['type'] = 1;

$array[2]['name'] = "typ3";
$array[2]['type'] = 3;

$sorted = mu_sort($array, "type");

print_r($sorted);
```

Permalänk: https://www.webforum.nu/p/1494407

## #3 — Matte, 2004-11-29T22:02Z

Annars tycker jag det bör funka med usort():

```php
function cmp($a, $b) {

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

}

usort($array, 'cmp');
```

Permalänk: https://www.webforum.nu/p/1497405

## #4 — WiZzLeR, 2004-11-29T22:38Z

du menar

```php
function cmp($a, $b)
{
   return strcmp($a['type'], $b['type']);
}

$array[0]['name'] = "typ2";
$array[0]['type'] = 2;

$array[1]['name'] = "typ1";
$array[1]['type'] = 1;

$array[2]['name'] = "typ3";
$array[2]['type'] = 3;

usort($array, "cmp");
```

Permalänk: https://www.webforum.nu/p/1497419

## #5 — Matte, 2004-11-30T07:07Z

Nej, jag menar:

```php
function cmp($a, $b) {

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

}

$array[0]['name'] = "typ2";
$array[0]['type'] = 2;

$array[1]['name'] = "typ1";
$array[1]['type'] = 1;

$array[2]['name'] = "typ3";
$array[2]['type'] = 3;

usort($array, "cmp");
```

 
Då kan du även sortera numeriska värden. Med strcmp kommer 2 att vara större än 12.

Permalänk: https://www.webforum.nu/p/1497455

## #6 — WiZzLeR, 2004-11-30T18:36Z

jahååå =)

Permalänk: https://www.webforum.nu/p/1497664

---

Tråden på webben: https://www.webforum.nu/amne/php/116996-problem-med-att-sortera-en-array
