Trie d'un tableau php
Résolu/Fermé
zaks91
Messages postés
12
Date d'inscription
dimanche 22 mai 2016
Statut
Membre
Dernière intervention
31 juillet 2016
-
Modifié par zaks91 le 19/07/2016 à 13:12
zaks91 Messages postés 12 Date d'inscription dimanche 22 mai 2016 Statut Membre Dernière intervention 31 juillet 2016 - 19 juil. 2016 à 14:21
zaks91 Messages postés 12 Date d'inscription dimanche 22 mai 2016 Statut Membre Dernière intervention 31 juillet 2016 - 19 juil. 2016 à 14:21
A voir également:
- Trie d'un tableau php
- Tableau croisé dynamique - Guide
- Tableau ascii - Guide
- Tableau word - Guide
- Comment trier un tableau excel - Guide
- Easy php - Télécharger - Divers Web & Internet
1 réponse
zaks91
Messages postés
12
Date d'inscription
dimanche 22 mai 2016
Statut
Membre
Dernière intervention
31 juillet 2016
19 juil. 2016 à 14:21
19 juil. 2016 à 14:21
Bon c'est bon j'ai juste fait une erreur j'ai récupérer la valeur de array_shift dans le même tableau donc voila un code qui marche si besoin.
<?php
@session_start();
// CREATION DU TABLEAU INITIAL
function creationTableau(){
$tab = array();
$tab[0] = 124;
$tab[1] = 102;
$tab[2] = 124;
$tab[3] = 100;
$tab[4] = 102;
return $tab;}
// TRIE D4UN TABLEAU ME RENVOIS UN TABLEAU AVEC LES OCCURENCES
function trie ($tabSans, $e){
$max=count($tabSans);
$i=0;
$j=0;
$existe=0;
while ($i<$max){
if ($tabSans[0] == $tabSans[$i] AND $i!=0 ) {
if ($e == 0)
$tab0[$j]=$tabSans[$i];
elseif ($e == 1) $tab1[$j]=$tabSans[$i];
$j=$j+1;
$existe=1;
}
$i=$i+1;
}
echo $e;
if ($existe == 1){
if ($e == 0){
$tab0[$j]=$tabSans[0];
$tabtab=$tab0;
}
elseif ($e == 1){
$tab1[$j]=$tabSans[0];
$tabtab=$tab1;
}
}
return $tabtab;
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
<title>test tableau</title>
</head>
<body>
<table style="width: 400px">
<tr>
<td colspan="4">Votre panier</td>
</tr>
<tr>
<td>Libellé</td>
<td>Quantité</td>
<td>Prix Unitaire</td>
<td>Action</td>
</tr>
<?php
$tab=creationTableau();
$tabSans = $tab;
$e=0;
if (count($tab)>0)
{
$max=count($tabSans);
// boucler pour trouver les occurrence de chaque élément
//d'un tableau tout en suppriment le 1er elmt a chaque tour,
for ($j=0; $j < $max ; $j++) {
$tab1= trie($tabSans, $e);
$nbArticles=count($tab1);
if ($nbArticles > 0)
{
$e=$e+1;
array_shift($tabSans); // supprimer le premier élément du tableau
//pour refaire une boucle et chercher de nouvelle occurence
for ($i=0 ;$i < $nbArticles ; $i++)
{
echo "<tr>";
echo "<td>".htmlspecialchars($tab1[$i])."</ td>";
echo "</tr>";
}
}
}
}
?>
</table>
</body>
</html>