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 :

modification tableau multidimensionnel


Sujet :

Langage PHP

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Octobre 2013
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2013
    Messages : 5
    Points : 4
    Points
    4
    Par défaut modification tableau multidimensionnel
    Bonjour

    Je cherche à transformer un tableau unidimentionnel en multidimentionnel en fonction de clés dynamiques

    Le tableau d'origine
    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
     
    $arr = array(
          0 => array(
                            "attributs_id" =>  "9",
                            "libelle"=> "Checkbox",
                            "famille" => "Choix",
                            "slug" => "input_checkbox"
             ),
          1 => array(
                            "attributs_id" =>  "10",
                            "libelle"=> "Input",
                            "famille" => "Texte",
                            "slug" => "input"
             ),
         2 => array(
                            "attributs_id" =>  "17",
                            "libelle"=> "Zone de texte",
                            "famille" => "Texte",
                            "slug" => "textearea"
             )
    );
    Le tableau des clés
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    $arg_list = array(
          0 => "famille",
          1 => "attributs_id"
    );
    Résultat attendu
    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
     
    $tResult = array( "Choix" =>
                                           array(  9 =>
                                                             array(
                                                                       "attributs_id" =>  "9",
                                                                       "libelle"=> "Checkbox",
                                                                       "famille" => "Choix",
                                                                       "slug" => "input_checkbox"
                                                                   )
                                                 ),
                            "Texte" =>
                                            array(  10 =>
                                                             array(
                                                                       "attributs_id" =>  "10",
                                                                       "libelle"=> "Input",
                                                                       "famille" => "Texte",
                                                                       "slug" => "input"
                                                                   ),
                                                       17 =>
                                                             array(
                                                                       "attributs_id" =>  "17",
                                                                       "libelle"=> "Zone de texte",
                                                                       "famille" => "Texte",
                                                                       "slug" => "textearea"
                                                                   )
                                                 ),
                          );
    Test non concluant
    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
     
    foreach($arr as $row) 
    {
    	$result = 'tResult';
    	foreach($arg_list as $v)
    	{
    		$result .= "['".$row[$v]."']";
    	}
     
    	$$result = $row;
    }
     
    var_dump($tResult);
     
    //affichage du var_dump : Array(0)
    Merci par avance de votre aide

  2. #2
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    Dés que tu écris "['" tu fais fausse piste. [ est un élément de syntaxe PHP, pas une chaine de caractère.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    $result = array();
    $lstArgs = array_reverse($arg_list);
    foreach($arr as $row) 
    	{
    	$data = $row;
    	foreach ($lstArgs as $key) {
    		$data = array($row[$key]=>$data);
    	}
    	$result = array_merge($result,$data);
    }
    N'oubliez pas de consulter les FAQ PHP et les cours et tutoriels PHP

  3. #3
    Candidat au Club
    Profil pro
    Inscrit en
    Octobre 2013
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2013
    Messages : 5
    Points : 4
    Points
    4
    Par défaut
    Merci sabotage, je comprends ton explication et je vois mon erreur

    mais si j'utilise ton 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
     
    $arr = array(
          0 => array(
                            "attributs_id" =>  "9",
                            "libelle"=> "Checkbox",
                            "famille" => "Choix",
                            "slug" => "input_checkbox"
             ),
          1 => array(
                            "attributs_id" =>  "10",
                            "libelle"=> "Input",
                            "famille" => "Texte",
                            "slug" => "input"
             ),
         2 => array(
                            "attributs_id" =>  "17",
                            "libelle"=> "Zone de texte",
                            "famille" => "Texte",
                            "slug" => "textearea"
             )
    );
     
    $arg_list = array(
          0 => "famille",
          1 => "attributs_id"
    );
     
            $result = array();
            $lstArgs = array_reverse($arg_list);
            foreach($arr as $row) 
            	{
            	$data = $row;
            	foreach ($lstArgs as $key) {
            		$data = array($row[$key]=>$data);
            	}
            	$result = array_merge($result,$data);
            }
     
     
     echo "<pre>";
     print_r($result);
    j'obtiens:
    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
     
    Array
    (
        [Choix] => Array
            (
                [9] => Array
                    (
                        [attributs_id] => 9
                        [libelle] => Checkbox
                        [famille] => Choix
                        [slug] => input_checkbox
                    )
     
            )
     
        [Texte] => Array
            (
                [17] => Array
                    (
                        [attributs_id] => 17
                        [libelle] => Zone de texte
                        [famille] => Texte
                        [slug] => textearea
                    )
     
            )
     
    )
    il manque attributs_id => 10, il me semble que la boucle ne conserve qu'un seul enfant, le dernier parcouru

    en tout cas merci j'ai une piste pour continuer la reflexion

  4. #4
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    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
    function array_merge_recursive_distinct ( array &$array1, array &$array2 )
    {
      $merged = $array1;
     
      foreach ( $array2 as $key => &$value )
      {
        if ( is_array ( $value ) && isset ( $merged [$key] ) && is_array ( $merged [$key] ) )
        {
          $merged [$key] = array_merge_recursive_distinct ( $merged [$key], $value );
        }
        else
        {
          $merged [$key] = $value;
        }
      }
     
      return $merged;
    }
     
    $result = array();
    foreach($arr as $row) 
    	{
    	$data = $row;
    	foreach (array_reverse($arg_list) as $key) {
    		$data = array($row[$key]=>$data);
    	}
    	$result =  array_merge_recursive_distinct($result, $data);
    }
    y'a peut être plus directe à faire mais ... je suis nul en recursivité
    N'oubliez pas de consulter les FAQ PHP et les cours et tutoriels PHP

  5. #5
    Candidat au Club
    Profil pro
    Inscrit en
    Octobre 2013
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2013
    Messages : 5
    Points : 4
    Points
    4
    Par défaut
    Parfait!!

    un grand merci

Discussions similaires

  1. Réponses: 2
    Dernier message: 08/12/2005, 14h41
  2. Réponses: 2
    Dernier message: 27/11/2005, 23h36
  3. Nombre de dimensions d'un tableau multidimensionnel
    Par Bruno75 dans le forum VB 6 et antérieur
    Réponses: 3
    Dernier message: 08/07/2005, 10h03
  4. type de donnée tableau multidimensionnel
    Par opheliegomes dans le forum Débuter
    Réponses: 2
    Dernier message: 03/02/2005, 12h29
  5. [langage] tableau multidimensionnel
    Par totox17 dans le forum Langage
    Réponses: 3
    Dernier message: 03/12/2002, 15h58

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