Follow along with the video below to see how to install our site as a web app on your home screen.
Anmerkung: This feature may not be available in some browsers.
Für solche Ausgaben kann man Wunderbar print_r() nehmenPHP:foreach($tiere as $blubb => $tier){ echo $tier."<br />"; }
<?php
$a = array('zebra', 'Bunker', 'tandem', 'Makake', 'dilema');
natcasesort($a);
echo '<pre>',print_r($a, true),'</pre>';
?>
Array
(
[1] => Bunker
[4] => dilema
[3] => Makake
[2] => tandem
[0] => zebra
)
$a = array('zebra', 'Bunker', 'tandem', 'Makake', 'dilema');
$b = array();
natcasesort($a);
foreach ($a as $val) {
$b[] = $val;
}
$a = $b
// ausgeben des Arrays
echo '<pre>',print_r($a, true),'</pre>';
Array
(
[0] => Bunker
[1] => dilema
[2] => Makake
[3] => tandem
[4] => zebra
)
/**
* Arrays alphabetisch sortieren (case-insensitive)
*
* @param array $arrInput
* @return array
*/
function sortMyArray($arrInput) {
if(false === is_array($arrInput)) {
return false;
}
if(false === natcasesort($arrInput)) {
return false;
}
$result = array();
foreach ($arrInput as $val) {
$result[] = $val;
}
return $result;
}
dslicer at maine dot rr dot com schrieb:03-Jun-2003 03:41
Something that should probably be documented is the fact that both natsort and natcasesort maintain the key-value associations of the array. If you natsort a numerically indexed array, a for loop will not produce the sorted order; a foreach loop, however, will produce the sorted order, but the indices won't be in numeric order. If you want natsort and natcasesort to break the key-value associations, just use array_values on the sorted array, like so:
natcasesort($arr);
$arr = array_values($arr);
Und die Variante mit array_values() hatte ich ja auch oben schon angemerkt. Klappt halt nur, wenn kein Wert innerhalb des Arrays doppelt vorkommt...This function implements a sort algorithm that orders alphanumeric strings in the way a human being would while maintaining key/value associations.
$array= array("img12.png", "Img3.png", "img4.png", "img1.png");
usort($array, "strnatcasecmp");
print_r($array);
Array
(
[0] => img1.png
[1] => Img3.png
[2] => img4.png
[3] => img12.png
)