sort multidimensional arrays with PHP and usort

  • German

Was?

Das hat mich schon lange gewurmt…

Wie sortiere ich ein multidimensionales Array nach Unterpunkten.

Im konkreten Fall ist es ein Array, das von CpmFetch (Thumbnail via WebSnapr: http://cpmfetch.fistfullofcode.com/) zurückgegeben wird (für maxiMEE).

Alle meine kläglichen Versuche mit array_multisort sind gescheitert, usort ist des Rätsels Lösung.

 

Wie?

Von CpmFetch erhält man etwas in der Art:

Array
(
    [0] => Array
        (
            [pFilepath] => userpics/10001/
            [pFilename] => Andreas_Wehr_Leinwand.jpg
            [pAid] => 2
            [pFilesize] => 1070798
            [pTitle] => Andreas Wehr Leinwand
            [pCaption] =>
            [pOwner_name] => admin
            [pOwnerId] => 1
            [pCtime] => 1190127402
            [pHits] => 13
            [pPid] => 141
            [pPic_Rating] => 0
            [pVotes] => 0
            [pWidth] => 1038
            [pHeight] => 1528
            [pUser1] =>
            [pUser2] =>
            [pUser3] =>
            [pUser4] =>
            [cCid] => 2
            [cName] => Testbilder
            [cDescription] =>
            [cPos] => 0
            [cParent] => 0
            [cThumb] => 5
            [uUser_lastvisit] => 2008-02-25 21:44:37
            [uUser_regdate] => 2006-12-04 10:29:48
            [uUser_email] =>
            [uUser_profile1] =>
            [uUser_profile2] =>
            [uUser_profile3] =>
            [uUser_profile4] =>
            [uUser_profile5] =>
            [uUser_profile6] =>
            [aAid] => 2
            [aTitle] => Test
            [aDescription] =>
            [aVisibility] => 0
            [aPos] => 100
            [aCategory] => 2
            [aThumb] => 0
            [aKeyword] =>
            [fullPathToThumb] => albums/userpics/10001/thumb_Andreas_Wehr_Leinwand.jpg
            [fullPathToNormal] => albums/userpics/10001/normal_Andreas_Wehr_Leinwand.jpg
            [fullPathToFull] => albums/userpics/10001/Andreas_Wehr_Leinwand.jpg
        )

    [1] => Array
        (
            [pFilepath] => userpics/10001/
            [pFilename] => muschelsuppe_suf_framed.jpg
            [pAid] => 2
            [pFilesize] => 287347
            [pTitle] =>
            [pCaption] =>
            [pOwner_name] => admin
            [pOwnerId] => 1
            [pCtime] => 1190046369
            [pHits] => 8
            [pPid] => 11
            [pPic_Rating] => 0
            [pVotes] => 0
            [pWidth] => 1000
            [pHeight] => 1000
            [pUser1] =>
            [pUser2] =>
            [pUser3] =>
            [pUser4] =>
            [cCid] => 2
            [cName] => Testbilder
            [cDescription] =>
            [cPos] => 0
            [cParent] => 0
            [cThumb] => 5
            [uUser_lastvisit] => 2008-02-25 21:44:37
            [uUser_regdate] => 2006-12-04 10:29:48
            [uUser_email] =>
            [uUser_profile1] =>
            [uUser_profile2] =>
            [uUser_profile3] =>
            [uUser_profile4] =>
            [uUser_profile5] =>
            [uUser_profile6] =>
            [aAid] => 2
            [aTitle] => Test
            [aDescription] =>
            [aVisibility] => 0
            [aPos] => 100
            [aCategory] => 2
            [aThumb] => 0
            [aKeyword] =>
            [fullPathToThumb] => albums/userpics/10001/thumb_muschelsuppe_suf_framed.jpg
            [fullPathToNormal] => albums/userpics/10001/normal_muschelsuppe_suf_framed.jpg
            [fullPathToFull] => albums/userpics/10001/muschelsuppe_suf_framed.jpg
        )

    )

Sortiert werden sollen jetzt die Bilder entweder nach Einstellungsdatum in die Galerie, oder nachh Titel. Jeweils aufsteigend und absteigend.

$sort_type definiert dabei die Art zu sortieren.

$sort_type = 2;

	//sort the array
	usort($images,sortImages);
	//the sort-function
	function sortImages($a,$b)
	{
		global $sort_type;

				switch ($sort_type) {
				case 1:
						if ($a[pCtime] == $b[pCtime]) {
				  		     return 0;
				 			   }
				    return ($a[pCtime] < $b[pCtime]) ? -1 : 1;
						break;
				case 2:
						if ($a[pCtime] == $b[pCtime]) {
				  		     return 0;
				 			   }
				    return ($a[pCtime] > $b[pCtime]) ? -1 : 1;
						break;
				case 3:
						if ($a[pTitle] == $b[pTitle]) {
				  		     return 0;
				 			   }
				    return strcmp($a[pTitle],$b[pTitle]);
						break;
				case 4:
						if ($a[pTitle] == $b[pTitle]) {
				  		     return 0;
				 			   }
				    return -strcmp($a[pTitle],$b[pTitle]);
						break;
				default:

						break;
				}

}

Mit

usort($images,sortImages); 

wird der Sortiervorgang angestoßen.

usort ruft dann jeweils die Funktion sortImages auf, um die beiden Array-Elemente zu vergleichen.

Es wird als Rückgabe eine Zahl kleiner, gleich oder größer 0 erwartet.

(siehe auch PHP-Manual Thumbnail via WebSnapr: http://us2.php.net/manual/en/function.usort.php)

Wird nach Datum sortiert, wird entsprechend die pCtime verglichen.

Bei der Sortierung nach Titel hilft strcmp.

 

Jetzt bin ich erleichtert, das maxiMEE Coppermine Plugin ist damit fast fertig :)


  • Stats

    Besucher online: 1
    Besucher heute: 15
    Besucher seit 29. März 2008: 6364
    21 Besucher haben sich diese Seite angesehen
  • Spam Blocked