phpday 2027 - Call for Papers

ArrayObject::ksort

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

ArrayObject::ksort — Sort the entries by key

Descrizione

public function ArrayObject::ksort(int $flags = SORT_REGULAR): true

Sorts the entries by key, maintaining key to entry correlations. This is useful mainly for associative arrays.

Nota:

Se due membri risultano uguali nel confronto, mantengono il loro ordine originale. Prima di PHP 8.0.0, il loro ordine relativo nell'array ordinato non era definito.

Elenco dei parametri

flags

Il secondo parametro opzionale flags può essere usato per modificare il comportamento dell'ordinamento usando questi valori:

Flag del tipo di ordinamento:

Valori restituiti

Restituisce sempre true.

Log delle modifiche

Versione Descrizione
8.3.0 Il tipo di ritorno ora è true, in precedenza era bool.

Esempi

Example #1 ArrayObject::ksort() example

<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
$fruitArrayObject = new ArrayObject($fruits);
$fruitArrayObject->ksort();

foreach ($fruitArrayObject as $key => $val) {
    echo "$key = $val\n";
}
 ?>

Il precedente esempio visualizzerà:

a = orange
b = banana
c = apple
d = lemon

Vedere anche:

+add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top