“Trier le tableau PHP” Réponses codées

Toi PHP par valeur de tableau associative

//php 7+
usort($inventory, function ($item1, $item2) {
    return $item1['price'] <=> $item2['price'];
});
Friendly Hawk

Trier le tableau PHP

<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
    echo "$key = $val\n";
}
?>
//Would output:
c = apple
b = banana
d = lemon
a = orange  
GelatinousMustard

tableau de commande PHP

<?php
$array = array("id" => 8, "id" =>1, "id" =>3, "id"=>2, "id" => 12, "id" =>19);
print_r($array->orderBy("id"));
?>
Light Loris

Array de tri PHP par valeur

$price = array();
foreach ($inventory as $key => $row)
{
    $price[$key] = $row['price'];
}
array_multisort($price, SORT_DESC, $inventory);
Silly Sardine

Trier du tableau PHP


<?php

$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
    echo $val;
}
/*
OUTPUT:
apple
banana
lemon
orange
*/
?>

Matteoweb

trier un tableau en php manuellement

// take an array with some elements
$array = array('a','z','c','b');
// get the size of array
$count = count($array);
echo "<pre>";
// Print array elements before sorting
print_r($array);
for ($i = 0; $i < $count; $i++) {
    for ($j = $i + 1; $j < $count; $j++) {
        if ($array[$i] > $array[$j]) {
            $temp = $array[$i];
            $array[$i] = $array[$j];
            $array[$j] = $temp;
        }
    }
}
echo "Sorted Array:" . "<br/>";
print_r($array);
Grotesque Giraffe

Réponses similaires à “Trier le tableau PHP”

Questions similaires à “Trier le tableau PHP”

Plus de réponses similaires à “Trier le tableau PHP” dans PHP

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code