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 :

Creation d'un tableau multidimentionel


Sujet :

Langage PHP

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    146
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2009
    Messages : 146
    Points : 72
    Points
    72
    Par défaut Creation d'un tableau multidimentionel
    Bonjour

    J'ai une liste de titre comme ce ci
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    	$array_title = array("fey palin" => 3 , "big theory" => 3 , "nyc through" => 3 , 
    							"jonas storm" => 3 , "bang theory" => 3  , "bang big" => 3 ,
    							"storm test" => 3 , "plain sahra" => 3 ,"mind woolf"  => 3, "mind virginia" => 3);
    J 'essaye de regrouper les titres qui ont un mot en commun par exemple bang theory et bang big sont lié par le mot bang

    sauf que je pense que j'ai un probleme dans la construction de mon tableau

    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_title = array("fey palin" => 3 , "big theory" => 3 , "nyc through" => 3 , 
    							"jonas storm" => 3 , "bang theory" => 3  , "bang big" => 3 ,
    							"storm test" => 3 , "plain sahra" => 3 ,"mind woolf"  => 3, "mind virginia" => 3);
     
     
     
    	$new_array = array();
     
    	$i = 0;
     
    	foreach($array_title as $title => $value){
     
    		if(count($new_array) > 0){
     
    			$words = explode(' ',$title);
    			foreach($new_array as $key => $value){
    				if(strpos($key, $words[0]) || strpos($key, $words[1])){
    					$new_array[$title][] = $key; // << ça ne rentre rien dans mon tableau
    				}else{
     
    					$new_array[$title] = array();
     
    				}
    			}
    		}else{
     
    			$new_array[$title] = array();
    		}
     
    		$i++;
    	}

  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
    Tu as une condition
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    if(count($new_array) > 0){
    comme
    c'est faux à chaque tour de boucle

    Je te propose :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    foreach ($array_title as $phrase=>$value) {
    	$words =  explode(' ', $phrase);
    	foreach ($words as $word) {
    		$new[$word][] = $phrase;
    	}
    }
    N'oubliez pas de consulter les FAQ PHP et les cours et tutoriels PHP

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    146
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2009
    Messages : 146
    Points : 72
    Points
    72
    Par défaut
    Je me suis peut etre mal exprimé et mal nommé mes variables

    Voila le 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
    28
    29
    30
    Array
    (
        [fey palin] => Array
            (
            )
        [big theory] => Array
            (
                [1] => bang theory    //lien avec le mot theory
                [2] => bang big       //lien avec le mot bang
            )
     
        [nyc through] => Array
            (
            )
     
        [jonas storm] => Array
            (
                [0] => storm test   //lien avec le mot storm
            )
     
        [plain sahra] => Array
            (
            )
     
        [mind woolf] => Array
            (
                [0] => mind virginia //lien avec le mot mind 
            )
     
    )

  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
    Dans ton exemple pourquoi "bang theory" ne va pas avec "bang big" ?
    N'oubliez pas de consulter les FAQ PHP et les cours et tutoriels PHP

  5. #5
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    146
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2009
    Messages : 146
    Points : 72
    Points
    72
    Par défaut
    Si justement bang theory va avec bang big

    Je voulais éviter de faire de me retrouver avec des sous sous tableaux
    "big theory" est lié a "bang theory" via le mot "theory" et "bang big " est lié a "bang theory" via le mot "bang"

Discussions similaires

  1. Réponses: 5
    Dernier message: 28/12/2006, 17h29
  2. [Tableaux] Création dynamique de tableau type array()
    Par Pahcixam dans le forum Langage
    Réponses: 7
    Dernier message: 29/11/2006, 15h34
  3. controle ActiveX pour la Creation d'un tableau
    Par sundjata dans le forum VB 6 et antérieur
    Réponses: 2
    Dernier message: 11/08/2006, 09h40
  4. Réponses: 1
    Dernier message: 30/01/2005, 12h00
  5. [VB.NET] Création d'un tableau en code behind
    Par netr dans le forum ASP.NET
    Réponses: 6
    Dernier message: 11/05/2004, 11h02

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