'Trying to sort my multi-dimensional array by price
I am very new to PhP and programming in general, I looked at similar questions and tried most of the offered solutions but couldn't find a way to apply it to my situation.
I made a filter option on a page, I am now trying to have the filtered results come out as Ascending by price. If I am to use a sort function, where should I be using it in the code for it to make sense?
<?php
require "voitures.php"; [my array][1]
if (isset($_GET["prixMin"])) {
$prixMin = $_GET["prixMin"];
$prixMax = $_GET["prixMax"];
$voitures2 = [];
foreach ($voitures as $voiture) {
if ($prixMin <= $voiture['prix'] && $prixMax >= $voiture['prix']) {
$voitures2[] = $voiture;
}
}
}
?>
<form action=" <?= $_SERVER['PHP_SELF'] ?>" method="GET">
<label for="prixMin">Prix minimal : </label>
<input type="text" name="prixMin" value="<?php if(isset($_GET['prixMin'])){ echo $_GET['prixMin']; }?>">
<label for="prixMax">Prix maximal : </label>
<input type="text" name="prixMax" value="<?php if(isset($_GET['prixMax'])){ echo $_GET['prixMax']; }?>">
<input type="submit" value="Rechercher">
</form>
<br>
<div class="g" style="grid-template-columns: repeat(4, max-content)">
<div class="t">Marques</div>
<div class="t">Modeles</div>
<div class="t">Annee</div>
<div class="t">Prix</div>
<?php if (isset($_GET['prixMin'])) { ?>
<?php foreach ($voitures2 as $voiture2) : ?>
<div class="l">
<div class="c dr"><?= $voiture2["marque"] ?></div>
<div class="c"><?= $voiture2["modele"] ?></div>
<div class="c"><?= $voiture2["annee"] ?></div>
<div class="c mi"><?= $voiture2["prix"] ?></div>
</div>
<?php endforeach ?>
<?php } else { ?>
<?php foreach ($voitures as $voiture) : ?>
<div class="l">
<div class="c dr"><?= $voiture["marque"] ?></div>
<div class="c"><?= $voiture["modele"] ?></div>
<div class="c"><?= $voiture["annee"] ?></div>
<div class="c mi"><?= $voiture["prix"] ?></div>
</div>
<?php endforeach ?>
<?php } ?>
</div>
</main>
<footer>
</footer>
</body>
</html>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
