Sorting multi-dimensional arrays in PHP

Some basic code examples.

Indexed array of indexed arrays

// Indexed array of indexed arrays
$albums = array (
    array( 'Toys in the Attic', 
	       '1975', 
	       'Aerosmith'
	),
    array( 'Back in Black', 
	       '1980', 
	       'AC/DC' ),
    array( 'Paranoid', 
	       '1970', 
	       'Black Sabbath' 
		  )
);

foreach ($albums as $key => $row) {
    $artist[$key] = $row[2]; 	
}

array_multisort($artist, SORT_ASC, $albums);

var_dump($artist);

foreach ($albums as $sub) {
	foreach ($sub as $k => $v) {
		echo $k . " - " . $v . "<br>"; 
	}
	echo "<br>";
}

Indexed array of associative arrays

// Indexed array of associative arrays
$albums = array(
    array( 'album' => 'Toys in the Attic', 
           'year' => '1975', 
	       'artist' => 'Aerosmith' 
		  ),
    array( 'album' => 'Back in Black', 
	       'year' => '1980', 
	       'artist' => 'AC/DC'
		  ),
    array( 'album' => 'Paranoid', 
	       'year' => '1970', 
	       'artist' => 'Black Sabbath' 
		  )
);

foreach ($albums as $key => $row) {
    $artist[$key] = $row['artist']; 	
}

array_multisort($artist, SORT_ASC, $albums);

foreach ($albums as $sub) {
	foreach ($sub as $k => $v) {
		echo $k . " - " . $v . "<br>"; 
	}
	echo "<br>";
}

Associative array of indexed arrays

// Associative array of indexed arrays
$albums = array (
    "Toys in the Attic"     => array( 'Toys in the Attic', 
	                                  '1975', 
	                                  'Aerosmith'
	                                ),
    "Back in Black"         => array( 'Back in Black', 
	                                  '1980', 
	                                  'AC/DC' 
							        ),
    "Paranoid"              => array( 'Paranoid', 
	                                  '1970', 
	                                  'Black Sabbath' 
		                            )
                );

foreach ($albums as $key => $row) {
    $album[$key] = $row[2]; 	
}

array_multisort($album, SORT_ASC, $albums);

foreach ($albums as $sub) {
	foreach ($sub as $k => $v) {
		echo $v . "<br>"; 
	}
	echo "<br>";
}

Associative array of associative arrays

// Associative array of associative arrays
$albums = array (
    "Toys in the Attic"     => array( 'title' => 'Toys in the Attic', 
	                                  'year' => '1975', 
	                                  'artist' => 'Aerosmith'
	                                ),
    "Back in Black"         => array( 'title' => 'Back in Black', 
	                                  'year' => '1980', 
	                                  'artist' => 'AC/DC' 
							        ),
    "Paranoid"              => array( 'title' => 'Paranoid', 
	                                  'year' => '1970', 
	                                  'artist' => 'Black Sabbath' 
		                            )
                );

foreach ($albums as $key => $row) {
    $album[$key] = $row['artist']; 	
}

array_multisort($album, SORT_ASC, $albums);

foreach ($albums as $sub) {
	foreach ($sub as $k => $v) {
		echo $v . "<br>"; 
	}
	echo "<br>";
}