IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Tri des données d'un tableau multidimensionnelle - array_multisort [PHP 5.5]


Sujet :

Langage PHP

  1. #1
    Membre à l'essai
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2017
    Messages
    26
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2017
    Messages : 26
    Points : 18
    Points
    18
    Par défaut Tri des données d'un tableau multidimensionnelle - array_multisort
    Bonjour,

    Je souhaiterai trier et ranger un tableau array_offerPlan[][] multidimensionnel selon la valeur [CAT1] (exemlpe: Soldes) et la valeur [CAT2](exemple:Institutionnelle), mais je bloque au niveau de la fonction me permettant le trie, pouvez -vous m'aider svp


    Voici mon code:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    $array_offerPlan[$m]=array('ID' => $m, 'CAT1' => $catTwo, 'CAT2' => $catOne, 'INFO' => $desc);
     
    foreach ($array_offerPlan as $key => $row) 
    {
    	$catTwo[$key]  = $row['CAT1'];
    	$catOne[$key]  = $row['CAT2'];
    }
     
    array_multisort($catTwo, SORT_ASC, $catOne, SORT_ASC, $array_offerPlan);
    var_dump($array_offerPlan);
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    array( 
                         [0] => Array ( 
                                              [ID] => 0 
                                              [CAT1] => Soldes
                                              [CAT2] => Institutionnelle
                                              [INFO] => mag + eStore)
                         [1] => Array ( 
                                              [ID] => 1 
                                              [CAT1] => Professionnelle 
                                              [CAT2] => Institutionnelle
                                              [INFO] =>mag) 
                         [2] => Array ( 
                                              [ID] => 2 
                                              [CAT1] => Professionnelle 
                                              [CAT2] => Institutionnelle
                                              [INFO] =>mag + eStore)  
                         [3] => Array ( 
                                              [ID] => 3 
                                              [CAT1] => Soldes
                                              [CAT2] => Locale
                                              [INFO] =>mag) 
                         [4] => Array ( 
                                              [ID] => 4 
                                              [CAT1] => Professionnelle 
                                              [CAT2] => Partenaire
                                              [INFO] =>mag + eStore) 
                         [5] => Array ( 
                                              [ID] => 5 
                                              [CAT1] => Soldes
                                              [CAT2] => Institutionnelle
                                              [INFO] =>mag) )
    et j'aimerai obtenir l'ordre suivant mais je n'y arrive pas:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    array ( 
     
                         [1] => Array ( 
                                              [ID] => 1 
                                              [CAT1] => Professionnelle 
                                              [CAT2] => Institutionnelle
                                              [INFO] =>mag) 
                         [2] => Array ( 
                                              [ID] => 2 
                                              [CAT1] => Professionnelle 
                                              [CAT2] => Institutionnelle
                                              [INFO] =>mag + eStore)  
                          [4] => Array ( 
                                              [ID] => 4 
                                              [CAT1] => Professionnelle 
                                              [CAT2] => Partenaire
                                              [INFO] =>mag + eStore) 
                         [0] => Array ( 
                                              [ID] => 0 
                                              [CAT1] => Soldes
                                              [CAT2] => Institutionnelle
                                              [INFO] => mag + eStore)
                         [5] => Array ( 
                                              [ID] => 5 
                                              [CAT1] => Soldes
                                              [CAT2] => Institutionnelle
                                              [INFO] =>mag) 
                         [3] => Array ( 
                                              [ID] => 3 
                                              [CAT1] => Soldes
                                              [CAT2] => Locale
                                              [INFO] =>mag) )

  2. #2
    Membre éprouvé Avatar de Willy_k
    Homme Profil pro
    Développeur Web
    Inscrit en
    Juin 2011
    Messages
    540
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juin 2011
    Messages : 540
    Points : 1 067
    Points
    1 067
    Par défaut
    Salut, regardez plutôt du côté de uasort
    Le bienfait n'est jamais perdu

  3. #3
    Membre à l'essai
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2017
    Messages
    26
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2017
    Messages : 26
    Points : 18
    Points
    18
    Par défaut
    Merci Willy_k pour votre réponse, j'ai essayé avec uasort mais je n'ai aucun tri qui s'applique. Est-ce que cette fonction est bien adapté au tableau multidimension?

    Voici le code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    <?php
     
    $fichier = 'fichier_global_offer.xml';
    $xml     = simplexml_load_file($fichier);
     
     
     
    $planOffer = $xml->xpath('//Niv2_Offre');
    $planOperation = $xml->xpath('//Niv1_Operation');
     
     
     
    $m=0;
    foreach ($planOffer as $offer)
    {
    	$code = $offer->{"Informations"}->{"Code"};
    	$offerName = $offer->{"Informations"}->{"Designation"};
    	$catOne = $offer->{"Informations"}->{"Categorie_Un"};
    	$catTwo = $offer->{"Informations"}->{"Categorie_Deux"};
    	$parentCode = $offer->{"Code"}->{"Code_Niv1"};
    	$beginDate = $offer->{"Criteres"}->{"Date_de_debut"};
    	$endDate = $offer->{"Criteres"}->{"Date_de_fin"};
    	$client = $offer->{"Criteres"}->{"Client"};
    	$execution = $offer->{"Criteres"}->{"Execution"};
    	$exclusion = $offer->{"Criteres"}->{"Articles_OUT"};
    	$canalDistrib = $offer->{"Criteres"}->{"canal_Distribution"};
    	if(empty($canalDistrib))
    	{
    		$canalDistrib = "mag";
    	}
     
    	foreach ($planOperation as $operation)
    	{
    		$opeCode = $operation->{"Code_operation"};
    		if($opeCode == $parentCode)
    		{
    			$operationName=$operation->{"Nom_operation"};
    		}
    		else
    		{
    			$operationName='non';
    		}
    	}
     
    	$desc='cible'.$client. ' - opération "'.$operationName.'" - code'.$code.' '.$offerName.' '.$execution.' // '.$canalDistrib.' Marques exclues : '.$exclusion.' Date de validité : du '.$beginDate.' au '.$endDate;
     
    	$array_offerPlan[$m]=array(
    		array('CAT1' => $catTwo, 
    			  'CAT2' => $catOne, 
    			'INFO' => $desc));
     
    	$m++;
    }
     
    var_dump($array_offerPlan);
     
    uasort($array_offerPlan, 'cmp');
    //$array_offerPlan2 = array_msort($array_offerPlan, array('CAT1' => SORT_DESC, 'CAT2' => SORT_DESC));
    //var_dump($array_offerPlan, $array_offerPlan2);
    var_dump($array_offerPlan);
     
    function cmp($a, $b) {
        if ($a == $b) {
            return 0;
        }
        return ($a < $b) ? -1 : 1;
    }
    /*function array_msort($array, $cols)
    {
        $colarr = array();
        foreach ($cols as $col => $order) {
            $colarr[$col] = array();
            foreach ($array as $k => $row) { $colarr[$col]['_'.$k] = strtolower($row[$col]); }
        }
        $eval = 'array_multisort(';
        foreach ($cols as $col => $order) {
            $eval .= '$colarr[\''.$col.'\'],'.$order.',';
        }
        $eval = substr($eval,0,-1).');';
        eval($eval);
        $ret = array();
        foreach ($colarr as $col => $arr) {
            foreach ($arr as $k => $v) {
                $k = substr($k,1);
                if (!isset($ret[$k])) $ret[$k] = $array[$k];
                $ret[$k][$col] = $array[$k][$col];
            }
        }
        return $ret;
    
    }*/
     
    ?>
    Voici le fichier xml:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    <?xml version="1.0" encoding="utf-8"?>
    <xmlGlobal>
      <Niv0_Campagne>
        <Nom_campagne>TEST ABE</Nom_campagne>
        <Code_campagne>2020</Code_campagne>
      </Niv0_Campagne>
      <Niv1_Operation>
        <Nom_operation>TEST 2 ABE</Nom_operation>
        <Code_operation>2021</Code_operation>
      </Niv1_Operation>
      <Niv2_Offre>
        <Code>
          <Code_Niv0>2020</Code_Niv0>
          <Code_Niv1>2021</Code_Niv1>
          <Code_Niv2>2022</Code_Niv2>
        </Code>
        <Informations>
          <ID>1</ID>
          <Date_de_création>09/06/2017</Date_de_création>
          <Code>2022</Code>
          <Compteur>0</Compteur>
          <Designation>TEST 3 ABE</Designation>
          <Benefice_client>Marques exclues : Anastasia, Becca, Dafni, Dyson</Benefice_client>
          <Financement>Mixte</Financement>
          <Statut>En cours de paramètrage</Statut>
          <Categorie_Un>Solde</Categorie_Un>
          <Categorie_Deux>Remise totale panier tous clients</Categorie_Deux>
          <Priorité>100_Operations_cumulables_niveau_1</Priorité>
        </Informations>
        <Criteres>
          <Transaction>Quantity_Item = 2</Transaction>
          <Date_de_debut>2017-07-09</Date_de_debut>
          <Date_de_fin>2017-07-16</Date_de_fin>
          <Articles_IN>1635 ; 2581 ; 4666 ; </Articles_IN>
          <Magasins_concernes>30280; 30160; </Magasins_concernes>
          <Execution>valable 1 seule fois</Execution>
          <Perimetre>Sélection de magasin</Perimetre>
          <canal_Distribution>mag + eStore</canal_Distribution>
        </Criteres>
        <Actions>
          <Type_de_remise>Action_Reduce Montant/Fixe 35</Type_de_remise>
          <Message>ligne1; ligne2; </Message>
        </Actions>
      </Niv2_Offre>
      <Niv0_Campagne>
        <Nom_campagne>TEST ABE</Nom_campagne>
        <Code_campagne>2029</Code_campagne>
      </Niv0_Campagne>
      <Niv1_Operation>
        <Nom_operation>TEST 2 ABE</Nom_operation>
        <Code_operation>2030</Code_operation>
      </Niv1_Operation>
      <Niv2_Offre>
        <Code>
          <Code_Niv0>2029</Code_Niv0>
          <Code_Niv1>2030</Code_Niv1>
          <Code_Niv2>2031</Code_Niv2>
        </Code>
        <Informations>
          <ID>2</ID>
          <Date_de_création>09/06/2017</Date_de_création>
          <Code>2031</Code>
          <Compteur>0</Compteur>
          <Designation>TEST 3 ABE</Designation>
          <Benefice_client>Marques exclues : Anastasia, Becca, Dafni, Dyson</Benefice_client>
          <Financement>Mixte</Financement>
          <Statut>En cours de paramètrage</Statut>
          <Categorie_Un>Solde</Categorie_Un>
          <Categorie_Deux>Message CRM</Categorie_Deux>
          <Priorité>100_Operations_cumulables_niveau_1</Priorité>
        </Informations>
        <Criteres>
          <Transaction>Quantity_Item = 2</Transaction>
          <Date_de_debut>2017-07-09</Date_de_debut>
          <Date_de_fin>2017-07-16</Date_de_fin>
          <Articles_IN>1635 ; 2581 ; 4666 ; </Articles_IN>
          <Magasins_concernes>30280; 30160; </Magasins_concernes>
          <Execution>valable 1 seule fois</Execution>
          <Perimetre>Sélection de magasin</Perimetre>
          <canal_Distribution/>
        </Criteres>
        <Actions>
          <Type_de_remise>Action_Reduce Montant/Fixe 35</Type_de_remise>
          <Message>ligne1; ligne2; </Message>
        </Actions>
      </Niv2_Offre>
      <Niv0_Campagne>
        <Nom_campagne>Test ABE </Nom_campagne>
        <Code_campagne>455</Code_campagne>
      </Niv0_Campagne>
      <Niv1_Operation>
        <Nom_operation>cible white - opération "BlackFriday Week" </Nom_operation>
        <Code_operation>456</Code_operation>
      </Niv1_Operation>
      <Niv2_Offre>
        <Code>
          <Code_Niv0>455</Code_Niv0>
          <Code_Niv1>456</Code_Niv1>
          <Code_Niv2>457</Code_Niv2>
        </Code>
        <Informations>
          <ID>3</ID>
          <Date_de_création>01/06/2017</Date_de_création>
          <Code>457</Code>
          <Compteur>0</Compteur>
          <Designation>Offre 'cible white - opération</Designation>
          <Benefice_client>Marques exclues : Anastasia, Becca, Dafni, Dyson</Benefice_client>
          <Financement>Mixte</Financement>
          <Statut>Active - Echue</Statut>
          <Categorie_Un>Institutionnelle</Categorie_Un>
          <Categorie_Deux>Message CRM</Categorie_Deux>
          <Priorité>100_Operations_cumulables_niveau_1</Priorité>
        </Informations>
        <Criteres>
          <Magasins_concernes/>
          <Execution/>
          <canal_Distribution/>
        </Criteres>
        <Actions>
          <Message/>
        </Actions>
      </Niv2_Offre>
      <ID>3</ID>
    </xmlGlobal>

  4. #4
    Membre éprouvé Avatar de Willy_k
    Homme Profil pro
    Développeur Web
    Inscrit en
    Juin 2011
    Messages
    540
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Juin 2011
    Messages : 540
    Points : 1 067
    Points
    1 067
    Par défaut
    je me suis inspiré de ça https://stackoverflow.com/questions/...answer-3233009 .
    Je nomme votre tableau $data
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    array_multisort(array_column($data, 'CAT1'), SORT_ASC, array_column($data, 'CAT2'), SORT_ASC, $data);
    Mais là, les index ne sont pas préservés.
    Pour uasort, oui il peut être adapté aux tableaux multidimensionnels.
    Le bienfait n'est jamais perdu

  5. #5
    Membre à l'essai
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2017
    Messages
    26
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2017
    Messages : 26
    Points : 18
    Points
    18
    Par défaut
    En effet cela fonctionne je vous remercie pour votre aide et votre dispo.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 2
    Dernier message: 25/05/2009, 09h38
  2. Réponses: 7
    Dernier message: 10/10/2008, 16h05
  3. [MySQL] Stocker des données dans un tableau
    Par LadyArwen dans le forum PHP & Base de données
    Réponses: 3
    Dernier message: 02/02/2006, 10h40
  4. Réponses: 3
    Dernier message: 05/12/2005, 02h30
  5. [JTable] Tri des données
    Par soulhouf dans le forum Composants
    Réponses: 7
    Dernier message: 08/09/2005, 14h01

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo