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
Bonjours, voila j'ai un tableau numérique et je veut le trie de la manier suivante j'ai essaie plusieurs truc mais bon sa marche pas :
dans un tableau t(), exemple t=124, 102, 124, 100, 102;
je veut trie les occurrence dans des tableaux différent comme
tab0=124, 124;
tab1=102;102;
tab2=100; // pour l'instant je m'on fiche un peut de celui la

voila une page test que j'ai utiliser mais je sais pas pourquoi cela ne donne rien la je suis a bout



<?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)
echo "<tr><td>Votre tableau est vide </ td></tr>";
else*/if ($nbArticles > 0)
{
$e=$e+1;
for ($i=0 ;$i < $nbArticles ; $i++)
{
echo "<tr>";
echo "<td>".htmlspecialchars($tab1[$i])."</ td>";
echo "</tr>";
}


}
$tabSans = array_shift($tabSans); // supprimer le premier élément du tableau pour refaire une boucle et chercher de nouvelle occurence
}
}
?>
</table>
</body>
</html>


des explication, des idée n'importe quoi serais le bienvenus merci
A voir également:

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
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>
0