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 :

[PHP 8] Recherche dans un tableau


Sujet :

Langage PHP

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé Avatar de arcane
    Inscrit en
    Avril 2003
    Messages
    313
    Détails du profil
    Informations forums :
    Inscription : Avril 2003
    Messages : 313
    Par défaut [PHP 8] Recherche dans un tableau
    Bonjour,
    Je cherche une solution qui fonctionne pour chercher du texte appartenant à un tableau dans un autre tableau.
    J'ai cette structure :
    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
    $tab1 = array();
    $tab2 = array();
     
    $tab1[0][0] = 'monfichier1';
    $tab1[1][0] = '123';
    $tab1[0][1] = 'monfichier2';
    $tab1[1][1] = '124';
    $tab1[0][2] = 'monfichier3';
    $tab1[1][2] = '125';
    $tab1[0][3] = 'monfichier4';
    $tab1[1][3] = '126';
     
    $tab2[0][0] = 'monfichier1';
    $tab2[1][0] = '123';
    $tab2[0][1] = 'monfichier2';
    $tab2[1][1] = '129';
    $tab2[0][2] = 'monfichier3';
    $tab2[1][2] = '125';
    $tab2[0][3] = 'monfichier5';
    $tab2[1][3] = '128';
    Le code suivant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    $key1 = array_search($tab1[0][0], $tab2); echo "\n <br /> key1 : " . $key1 . " // tab1[0][0] : " . $tab1[0][0];
    $key2 = array_search($tab1[0][1], $tab2); echo "\n <br /> key2 : " . $key2 . " // tab1[0][1] : " . $tab1[0][1];
    $key3 = array_search($tab1[0][2], $tab2); echo "\n <br /> key3 : " . $key3 . " // tab1[0][2] : " . $tab1[0][2];
    $key4 = array_search($tab1[0][3], $tab2); echo "\n <br /> key4 : " . $key4 . " // tab1[0][3] : " . $tab1[0][3];
     
    if (in_array($tab1[0][0], $tab2)) {echo "\n <br /> key1 : " . $key1 . " // tab1[0][0] : " . $tab1[0][0];} else {echo "\n <br />1 pas trouvé";};
    if (in_array($tab1[0][1], $tab2)) {echo "\n <br /> key2 : " . $key2 . " // tab1[0][1] : " . $tab1[0][1];} else {echo "\n <br />2 pas trouvé";};
    if (in_array($tab1[0][2], $tab2)) {echo "\n <br /> key3 : " . $key3 . " // tab1[0][2] : " . $tab1[0][2];} else {echo "\n <br />3 pas trouvé";};
    if (in_array($tab1[0][3], $tab2)) {echo "\n <br /> key4 : " . $key4 . " // tab1[0][3] : " . $tab1[0][3];} else {echo "\n <br />4 pas trouvé";};
    Rien ne fonctionne.

    Résultat :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    key1 : // tab1[0][0] : monfichier1
    key2 : // tab1[0][1] : monfichier2
    key3 : // tab1[0][2] : monfichier3
    key4 : // tab1[0][3] : monfichier4
    1 pas trouvé
    2 pas trouvé
    3 pas trouvé
    4 pas trouvé
    En résultat, je m'attends à trouver les 3 premiers fichiers.
    Pourriez-vous m'expliquer ce qui ne va pas ?

    Merci

  2. #2
    Membre éclairé Avatar de arcane
    Inscrit en
    Avril 2003
    Messages
    313
    Détails du profil
    Informations forums :
    Inscription : Avril 2003
    Messages : 313
    Par défaut
    Je pense que c'est le tableau à 2 dimensions qui pose problème, car :
    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
    $tab3[0] = 'monfichier1';
    $tab3[1] = 'monfichier2';
    $tab3[2] = 'monfichier3';
    $tab3[3] = 'monfichier4';
     
    $tab4[0] = 'monfichier1';
    $tab4[1] = 'monfichier2';
    $tab4[2] = 'monfichier3';
    $tab4[3] = 'monfichier5';
     
    $key1 = array_search($tab3[0], $tab4); echo "\n <br /> key1 : " . $key1 . " // tab3[0] : " . $tab3[0];
    $key2 = array_search($tab3[1], $tab4); echo "\n <br /> key2 : " . $key2 . " // tab3[1] : " . $tab3[1];
    $key3 = array_search($tab3[2], $tab4); echo "\n <br /> key3 : " . $key3 . " // tab3[2] : " . $tab3[2];
    $key4 = array_search($tab3[3], $tab4); echo "\n <br /> key4 : " . $key4 . " // tab3[3] : " . $tab3[3];
     
    if (in_array($tab3[0], $tab4)) {echo "\n <br /> key1 : " . $key1 . " // tab3[0] : " . $tab3[0];} else {echo "\n <br />1 pas trouvé";};
    if (in_array($tab3[1], $tab4)) {echo "\n <br /> key2 : " . $key2 . " // tab3[1] : " . $tab3[1];} else {echo "\n <br />2 pas trouvé";};
    if (in_array($tab3[2], $tab4)) {echo "\n <br /> key3 : " . $key3 . " // tab3[2] : " . $tab3[2];} else {echo "\n <br />3 pas trouvé";};
    if (in_array($tab3[3], $tab4)) {echo "\n <br /> key4 : " . $key4 . " // tab3[3] : " . $tab3[3];} else {echo "\n <br />4 pas trouvé";};
    donne :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    key1 : 0 // tab3[0] : monfichier1
    key2 : 1 // tab3[1] : monfichier2
    key3 : 2 // tab3[2] : monfichier3
    key4 : // tab3[3] : monfichier4
    key1 : 0 // tab3[0] : monfichier1
    key2 : 1 // tab3[1] : monfichier2
    key3 : 2 // tab3[2] : monfichier3
    4 pas trouvé

  3. #3
    Expert confirmé
    Avatar de mathieu
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    10 702
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 10 702
    Par défaut
    il existe des fonctions php qui fait ce genre de traitement des tableaux. essayez ce code :
    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    $liste = ["fichier1", "fichier2", "fichier3"];
    $cherche = ["fichier2", "fichier_pas_là"];
     
    // affiche les fichiers trouvés : "fichier2"
    var_export(array_intersect($liste, $cherche));
     
    // affiche les fichiers pas trouvés : "fichier_pas_là"
    var_export(array_diff($cherche, $liste));

    la documentation des fonctions est là :
    https://www.php.net/manual/fr/function.array-diff.php
    https://www.php.net/manual/fr/functi...-intersect.php

  4. #4
    Membre éclairé Avatar de arcane
    Inscrit en
    Avril 2003
    Messages
    313
    Détails du profil
    Informations forums :
    Inscription : Avril 2003
    Messages : 313
    Par défaut
    Merci pour la réponse.
    array_intersect ne m'intéresse pas, il cherche les valeurs présentes dans les 2 tableaux. Et il prend des tableaux en argument.
    array_diff est sensé faire ce que je veux sauf qu'il prend aussi des tableaux. et je veux chercher une chaine dans un tableau, en l'occurence la première colonne. La 2eme ne m'intéresse pas pour cette recherche.

    Mais array_diff ne semble pas fonctionner pour les tableaux à 2 dimensions, et il cherche aussi dans la 2eme colonne :
    Le code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    var_export(array_intersect($tab2, $tab1));
    echo "<br />\n";
    var_export(array_diff($tab1, $tab2));
    renvoit :

    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
    Warning: Array to string conversion in F:\www\alertes\testtab.php on line 74
    Warning: Array to string conversion in F:\www\alertes\testtab.php on line 74
    Warning: Array to string conversion in F:\www\alertes\testtab.php on line 74
    Warning: Array to string conversion in F:\www\alertes\testtab.php on line 74
    Warning: Array to string conversion in F:\www\alertes\testtab.php on line 74
    Warning: Array to string conversion in F:\www\alertes\testtab.php on line 74
    Warning: Array to string conversion in F:\www\alertes\testtab.php on line 74
    Warning: Array to string conversion in F:\www\alertes\testtab.php on line 74
    array ( 0 => array ( 0 => 'monfichier1', 1 => 'monfichier2', 2 => 'monfichier3', 3 => 'monfichier5', ), 1 => array ( 0 => '123', 1 => '129', 2 => '125', 3 => '128', ), )
     
    Warning: Array to string conversion in F:\www\alertes\testtab.php on line 76
    Warning: Array to string conversion in F:\www\alertes\testtab.php on line 76
    Warning: Array to string conversion in F:\www\alertes\testtab.php on line 76
    Warning: Array to string conversion in F:\www\alertes\testtab.php on line 76
    array ( )
    Merci

  5. #5
    Expert confirmé Avatar de CosmoKnacki
    Homme Profil pro
    Justicier interdimensionnel
    Inscrit en
    Mars 2009
    Messages
    2 987
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Justicier interdimensionnel

    Informations forums :
    Inscription : Mars 2009
    Messages : 2 987
    Par défaut
    La liste des noms de fichier se trouve à l'indice 0 de $tab2. Il suffit d'y faire directement ta recherche:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    if ( in_array($tab1[0][0], $tab2[0]) ) { //...

  6. #6
    Membre éclairé Avatar de arcane
    Inscrit en
    Avril 2003
    Messages
    313
    Détails du profil
    Informations forums :
    Inscription : Avril 2003
    Messages : 313
    Par défaut
    Merci c'est ca. J'ai toujours un peu de mal avec les tableaux, surtout au niveau de la syntaxe.

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

Discussions similaires

  1. [VBA-E]Recherche dans un tableau
    Par Zebulon777 dans le forum Macros et VBA Excel
    Réponses: 49
    Dernier message: 05/07/2006, 11h35
  2. Recherche dans un tableau
    Par Bes74 dans le forum Access
    Réponses: 5
    Dernier message: 04/07/2006, 18h26
  3. [PHP-JS] Rechercher dans une page
    Par eric41 dans le forum Langage
    Réponses: 7
    Dernier message: 08/05/2006, 11h05
  4. [VBA-E] recherche dans un tableau
    Par tibss dans le forum Macros et VBA Excel
    Réponses: 33
    Dernier message: 03/05/2006, 18h52
  5. URGENt: recherche dans un tableau trié par ordre alphabetiqu
    Par JulPop dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 12/02/2005, 18h21

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