Un array dans un array

grandpa0006
Invité n'ayant pas de compte PHPfrance

17 mars 2008, 10:30

Bonjour,

Voila mon probleme. J'ai le code ci-dessous qui me permet de recuperer la valeur de $tablo[] dans un array.
Visuellement cela donne cela :

Array (
[0] => Array ( [0] => 501 )
[1] => Array ( [0] => 311 )
[2] => Array ( [0] => 312 )
[3] => Array ( [0] => 114 )
[4] => Array ( [0] => 300 )
=>)

Dans le bas du code ce que je desire tester avec mon in_array c'est la valeur du tableau dans le tableau en gros je veux savoir si par exemple :
if (in_array("312",$tablo))
{echo "$res1['pnj_base_ref']</br>";}

Merci de votre aide .....

Code : Tout sélectionner

<?php // 1.2 - LISTE LES RESULTATS ENREGISTRES EN FONCTION DE l'ID DE LA BASE // VOIR LES RESULTATS ENREGISTRES $resultat = "SELECT pnj_base_ref,pnj_base_titre,pnj_base_date,pnj_base_cat FROM pnj_base ORDER BY pnj_base_date"; $res = mysql_query($resultat); $tablo = array(); while ($res1 = mysql_fetch_array($res)) { // DESERIALIZATION DES N° DE CATEGORIE ET INTEGRATION SOUS FORME DE TABLEAU $tablo[] = unserialize($res1['pnj_base_cat']); } print_r($tablo); // 1.1 - LISTE DES THEMES EN PARTANT DE LA GAUCHE // REQUETE $r1 = mysql_query("SELECT pnjcat_ref,pnjcat_niveau,pnjcat_txt,pnjcat_droite,pnjcat_gauche FROM pnj_cat ORDER BY pnjcat_gauche"); // BOUCLE while ($h1 = mysql_fetch_array($r1)) { //AFFICHAGE RESULTAT {echo '<strong>'.$h1[2].'</strong>'.' (id:'.$h1[0].')'.' - '.$h1[4].'/'.$h1[3].'</br>';} // LISTE LES RESULTATS DES DONNEES if (in_array($h1[0],$tablo)) {echo '<span class="texte_base_admin">'."&nbsp;&nbsp;".$res1['pnj_base_titre']."</span></br>";} } ?>

Avatar du membre
Modérateur PHPfrance
Modérateur PHPfrance | 10684 Messages

17 mars 2008, 11:27

Si tu utilises une version de php supérieure à la 4.2.0, tu peux passer un tableau en argument de in_array() (voir la doc de la fonction)

Tu dois donc pouvoir faire :
if ( in_array( array(0=>312), $tablo) ) 
Ce n'est pas en améliorant la bougie que l'on a inventé l'ampoule...