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

EDI, CMS, Outils, Scripts et API PHP Discussion :

Tableaux php, array to string


Sujet :

EDI, CMS, Outils, Scripts et API PHP

  1. #1
    Membre éclairé Avatar de Lekno
    Femme Profil pro
    Étudiant
    Inscrit en
    Septembre 2010
    Messages
    883
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 35
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Septembre 2010
    Messages : 883
    Points : 761
    Points
    761
    Par défaut Tableaux php, array to string
    Bonjour à tous, j'ai un petit soucis sur ce morceaux de code, je ne comprend pas le "array" to string convertion. Je passe pourtant bien par un foreach

    Sauriez vous m'indiquer une piste pour mon erreur ?

    Merci

    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    	if(isset($txt_pluriel)){
    		$mot_synonyme[] = mot_nom($txt_pluriel);
    	}
    	if(isset($mot_a_transformer[0])){
    		$mot_synonyme[] = mot_nom($mot_a_transformer[0]);
    	}
    }
    echo "<p>Type de variable :".gettype($mot_synonyme)."<BR />";
     
    foreach ($mot_synonyme as $val) {
        echo $val."<BR />";
    }
    echo "</p>";
    Vous pouvez avoir un aperçu de la sortie sur le lien ci-dessous, ( le forum n'affiche pas l'image via [img])

    http://s13.postimg.org/dlfs5l11z/script_sortie.jpg

  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
    je ne comprend pas le "array" to string convertion
    Étant donné que tu ne nous dis pas de quoi tu parles, nous non plus

    Si $mot_synonyme contient des tableaux, quand tu fais le echo, ça coince.

  3. #3
    Membre actif
    Homme Profil pro
    Développeur Web
    Inscrit en
    Mai 2012
    Messages
    131
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Mai 2012
    Messages : 131
    Points : 242
    Points
    242
    Par défaut
    fais un var_dump sur la variable $val, ta fonction mot_nom retourne surement un array, du coup tu as un notice suite a echo array

  4. #4
    Membre éclairé Avatar de Lekno
    Femme Profil pro
    Étudiant
    Inscrit en
    Septembre 2010
    Messages
    883
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 35
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Septembre 2010
    Messages : 883
    Points : 761
    Points
    761
    Par défaut
    Citation Envoyé par selmouadin Voir le message
    fais un var_dump sur la variable $val, ta fonction mot_nom retourne surement un array, du coup tu as un notice suite a echo array
    Citation Envoyé par sabotage
    Étant donné que tu ne nous dis pas de quoi tu parles, nous non plus

    Si $mot_synonyme contient des tableaux, quand tu fais le echo, ça coince.
    Lorsque je fais un var_dump j'ai bien mon contenu, je passe pourtant bien par un foreach pour tenter de lister mon tableau ? ( Je me trompe ? )

    Au vu du code présenté ci-dessus et du screen disponible en bas de mon premier post, qu'utiliseriez vous pour lister le contenu des tableaux ?

  5. #5
    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
    Que donne le var_dump de $mot_synonyme ?

  6. #6
    Membre éclairé Avatar de Lekno
    Femme Profil pro
    Étudiant
    Inscrit en
    Septembre 2010
    Messages
    883
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 35
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Septembre 2010
    Messages : 883
    Points : 761
    Points
    761
    Par défaut
    un VAR_DUMP sur mon mot_synonyme me retourne le résultat suivant :

    Citation Envoyé par php
    array(12) { [0]=> array(1) { [0]=> string(40) "Valeur de la chaine mot_nom() vide
    " } [1]=> array(1) { [0]=> string(40) "Valeur de la chaine mot_nom() vide
    " } [2]=> array(14) { [0]=> string(62) "avancer" [1]=> string(63) "echapper" [2]=> string(61) "ramper" [3]=> string(62) "chasser" [4]=> string(62) "inserer" [5]=> string(60) "riper" [6]=> string(61) "couler" [7]=> string(65) "introduire" [8]=> string(60) "skier" [9]=> string(64) "coulisser" [10]=> string(61) "mettre" [11]=> string(61) "tomber" [12]=> string(62) "deraper" [13]=> string(62) "patiner" } [3]=> array(14) { [0]=> string(62) "avancer" [1]=> string(63) "echapper" [2]=> string(61) "ramper" [3]=> string(62) "chasser" [4]=> string(62) "inserer" [5]=> string(60) "riper" [6]=> string(61) "couler" [7]=> string(65) "introduire" [8]=> string(60) "skier" [9]=> string(64) "coulisser" [10]=> string(61) "mettre" [11]=> string(61) "tomber" [12]=> string(62) "deraper" [13]=> string(62) "patiner" } [4]=> array(21) { [0]=> string(60) "barge" [1]=> string(64) "chalutier" [2]=> string(62) "steamer" [3]=> string(61) "barque" [4]=> string(62) "coche-1" [5]=> string(70) "transatlantique" [6]=> string(68) "bateau-mouche" [7]=> string(66) "embarcation" [8]=> string(63) "vaisseau" [9]=> string(63) "caboteur" [10]=> string(61) "navire" [11]=> string(61) "vapeur" [12]=> string(64) "caravelle" [13]=> string(58) "nef" [14]=> string(60) "voile" [15]=> string(60) "cargo" [16]=> string(63) "paquebot" [17]=> string(60) "yacht" [18]=> string(62) "chaland" [19]=> string(61) "rafiot" [20]=> string(59) "yole" } [5]=> array(21) { [0]=> string(60) "barge" [1]=> string(64) "chalutier" [2]=> string(62) "steamer" [3]=> string(61) "barque" [4]=> string(62) "coche-1" [5]=> string(70) "transatlantique" [6]=> string(68) "bateau-mouche" [7]=> string(66) "embarcation" [8]=> string(63) "vaisseau" [9]=> string(63) "caboteur" [10]=> string(61) "navire" [11]=> string(61) "vapeur" [12]=> string(64) "caravelle" [13]=> string(58) "nef" [14]=> string(60) "voile" [15]=> string(60) "cargo" [16]=> string(63) "paquebot" [17]=> string(60) "yacht" [18]=> string(62) "chaland" [19]=> string(61) "rafiot" [20]=> string(59) "yole" } [6]=> string(7) "mangeai" [7]=> string(8) "mangeais" [8]=> array(1) { [0]=> string(40) "Valeur de la chaine mot_nom() vide
    " } [9]=> array(1) { [0]=> string(40) "Valeur de la chaine mot_nom() vide
    " } [10]=> string(10) "sauterelle" [11]=> string(11) "sauterelles" }

  7. #7
    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
    Comme prévu, ton tableau contient des tableaux.

  8. #8
    Membre éclairé Avatar de Lekno
    Femme Profil pro
    Étudiant
    Inscrit en
    Septembre 2010
    Messages
    883
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 35
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Septembre 2010
    Messages : 883
    Points : 761
    Points
    761
    Par défaut
    Mon problème est que les fonctions retournant mon tableau synonyme, peuvent me retourner 0, 1 ou X éléments, comment peux tu procéder pour retourner un résultat sous forme de chaîne ?

    Actuellement mes fonctions fonctionne comme ceci, donc tu as effectivement raison pour les tableaux (chose que je n'avais pas du tout pensé... )

    Sais tu m'indiquer comment l'on peut rerourner plusieurs résultat via une fonction ?

    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    $mot_synonyme[] = $element;
    return $mot_synonyme;

  9. #9
    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
    Je n'ai pas compris ta question.

    Tu devrais en tout cas par exemple t'inquiéter que ton tableau contienne des "Valeur de la chaine mot_nom() vide" par exemple.

  10. #10
    Membre actif
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2006
    Messages
    152
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

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

    Informations forums :
    Inscription : Janvier 2006
    Messages : 152
    Points : 222
    Points
    222
    Par défaut
    sinon tu peux faire retourner, un tableau contenant 0 1 ou X elements, par ta fonction mot_nom.


    Et tu fait un merge avec ton array $mot_synonyme.

  11. #11
    Membre éclairé Avatar de Lekno
    Femme Profil pro
    Étudiant
    Inscrit en
    Septembre 2010
    Messages
    883
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 35
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Septembre 2010
    Messages : 883
    Points : 761
    Points
    761
    Par défaut
    J'ai repris mon code ce matin. A ce stade VAR_DUMP me retourne cela.

    Je me retrouve avec array et string.... savez vous me donner une idée pour exploiter ce retour ? (Le fait d'avoir des s"string" et des "array" ne risque il pas de poser probleme ?)


    array(14) { [0]=> string(62) "avancer" [1]=> string(63) "echapper" [2]=> string(61) "ramper" [3]=> string(62) "chasser" [4]=> string(62) "inserer" [5]=> string(60) "riper" [6]=> string(61) "couler" [7]=> string(65) "introduire" [8]=> string(60) "skier" [9]=> string(64) "coulisser" [10]=> string(61) "mettre" [11]=> string(61) "tomber" [12]=> string(62) "deraper" [13]=> string(62) "patiner" } array(23) { [0]=> string(63) "absorber" [1]=> string(60) "laper" [2]=> string(63) "se-taper" [3]=> string(62) "arroser" [4]=> string(60) "lever" [5]=> string(62) "siffler" [6]=> string(61) "avaler" [7]=> string(62) "picoler" [8]=> string(62) "siroter" [9]=> string(65) "biberonner" [10]=> string(61) "pomper" [11]=> string(60) "subir" [12]=> string(64) "consommer" [13]=> string(65) "s-abreuver" [14]=> string(60) "sucer" [15]=> string(62) "ecluser" [16]=> string(63) "savourer" [17]=> string(63) "trinquer" [18]=> string(71) "etancher-sa-soif" [19]=> string(68) "se-desalterer" [20]=> string(60) "vider" [21]=> string(65) "ingurgiter" [22]=> string(68) "se-rafraichir" } array(1) { [0]=> string(59) "plus" } array(24) { [0]=> string(61) "abuser" [1]=> string(63) "capturer" [2]=> string(62) "prendre" [3]=> string(65) "admonester" [4]=> string(61) "choper" [5]=> string(66) "reprimander" [6]=> string(63) "agripper" [7]=> string(65) "contracter" [8]=> string(64) "reprocher" [9]=> string(64) "atteindre" [10]=> string(60) "duper" [11]=> string(61) "saisir" [12]=> string(60) "avoir" [13]=> string(62) "gronder" [14]=> string(64) "s-emparer" [15]=> string(61) "berner" [16]=> string(61) "happer" [17]=> string(64) "sermonner" [18]=> string(61) "blamer" [19]=> string(62) "leurrer" [20]=> string(65) "subtiliser" [21]=> string(61) "capter" [22]=> string(64) "mystifier" [23]=> string(62) "tromper" } Request was Generated in 12

  12. #12
    Membre actif
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2006
    Messages
    152
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

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

    Informations forums :
    Inscription : Janvier 2006
    Messages : 152
    Points : 222
    Points
    222
    Par défaut
    Si, comme je te le disais avant, enfin le plus simple serait de t'arranger pour n'avoir que des string dans ton array $mot_synonyme !


    Personnelement je ferais retourner un tableau par ma fonction mot_nom() (même vide ou un seul element).

    Et je concaténerais avec $mot_synonyme.

    Si tu ne t'en sort pas met nous le code de mot_nom()

  13. #13
    Membre éclairé Avatar de Lekno
    Femme Profil pro
    Étudiant
    Inscrit en
    Septembre 2010
    Messages
    883
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 35
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Septembre 2010
    Messages : 883
    Points : 761
    Points
    761
    Par défaut
    C'est résolu, en effet j'avais une concaténation de tableau qui me rendait l'exploitation du return très compliqué. J'ai repris mes fonctions afin de retourner plus facilement les donnés que je souhaitais.

    Dans le code que je présentais, cela été trop brouillon.

    Merci à ceux qui m'ont apporté un coup de main

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

Discussions similaires

  1. Réponses: 4
    Dernier message: 18/02/2011, 16h55
  2. [Tableaux] Tableau (array) PHP/ foreach
    Par KOUTO dans le forum Langage
    Réponses: 2
    Dernier message: 01/06/2009, 00h54
  3. Réponses: 2
    Dernier message: 10/04/2008, 18h16
  4. Réponses: 2
    Dernier message: 28/12/2007, 12h09
  5. [Tableaux] PHP, Array et réaffichage
    Par kagura dans le forum Langage
    Réponses: 11
    Dernier message: 29/08/2006, 14h07

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