Page 1 sur 1

array et foreach ligne par ligne

Posté : 11 avr. 2013, 18:43
par tom_nameless
Bonjour,

Pas bien compliqué en principe.. mais je bloque !
j'ai un tableau :

Code : Tout sélectionner

Array ( [ref_porte] => Array ( [0] => porte bureau DG [1] => porte bureau DG [2] => porte bureau DG [3] => porte bureau DG ) [longueur] => Array ( [0] => [1] => K [2] => K [3] => K ) [bouton] => Array ( [0] => [1] => K [2] => K [3] => K ) [ref_cylindre] => Array ( [0] => DK/30-30 [1] => DK/30-30 [2] => DK/30-30 [3] => DK/30-30 ) )
et j'aimerai afficher ce tableau ligne par ligne :
premiere ligne pour l'id 1
l'id 2 en seconde ligne
...

j'ai fait ça mais ça ne marche pô #-o
foreach ($_SESSION['liste'] as $key => $value) {
	// Affichage codes et libellés articles
	echo "article ".$key." : ";
		foreach ($value as $key2 => $value2) {
	echo $value2."<br />";}
	}

Re: array et foreach ligne par ligne

Posté : 11 avr. 2013, 20:17
par sirakawa
Il faudrait d'abord construire le tableau sans erreurs de syntaxe:
<?PHP
$t= array (
	"ref_porte" => Array 
	( 
		0 => "porte bureau DG",
	 	1 => "porte bureau DG",
		2 => "porte bureau DG",
	 	3 => "porte bureau DG" ),
	"longueur" => Array 
	( 
		0 => "K", 
		2 => "K",
	 	3 => "K" 
	),
	"bouton" => Array 
	( 
		0 => "K", 
		2 => "K", 
		3 => "K" 
	),
	"ref_cylindre" => Array 
	( 
	0 => "DK/30-30",
	1 => "DK/30-30",
	2 => "DK/30-30", 
	3 => "DK/30-30" 
	) 
) ;
print_r($t);
?>
et pour cela lire :
http://fr2.php.net/manual/fr/language.types.array.php
en distinguant la construction du tableau associatif et l'exploitation de ce tableau(dans la doc par var_dump()...

Re: array et foreach ligne par ligne

Posté : 11 avr. 2013, 20:29
par moogli
salut,

j'ai corrigé les données manquantes, le code peux être ainsi:
<?php
$tab = [
    'ref_porte' => ['0' => 'porte bureau DG', '1' => 'porte bureau DG', '2' => 'porte bureau DG', '3' => 'porte bureau DG'],
    'longueur' => ['0' => 'K', '1' => 'K', '2' => 'K', '3' => 'K'],
    'bouton' => ['0' => 'k', '1' => 'K', '2' => 'K', '3' => 'K'],
    'ref_cylindre' => ['0' => 'DK / 30 - 30', '1' => 'DK / 30 - 30', '2' => 'DK / 30 - 30', '3' => 'DK / 30 - 30']
];


foreach ($tab as $key => $val) {
    echo $key, ' : ', implode(', ', $val), '<br />';
}
résultat
ref_porte : porte bureau DG, porte bureau DG, porte bureau DG, porte bureau DG
longueur : K, K, K, K
bouton : k, K, K, K
ref_cylindre : DK / 30 - 30, DK / 30 - 30, DK / 30 - 30, DK / 30 - 30
après si tu précise plus ta demande il y a surement moyen de faire mieux, par exemple afficher sous forme de tableaux etc etc

@+

Re: array et foreach ligne par ligne

Posté : 12 avr. 2013, 12:42
par tom_nameless
Merci de vous pencher sur mon cas !

Code : Tout sélectionner

Array ( [ref_porte] => Array ( [0] => porte A [1] => porte B ) [longueur] => Array ( [0] => 30-30 [1] => 10-30 ) [bouton] => Array ( [0] => K [1] => K ) [ref_cylindre] => Array ( [0] => DK/30-30 [1] => DK/10-30 ) )
En fait j'aimerais avoir au final un tableau sous la forme : ref_porte,longueur,bouton,ref_cylindre
exemple :
[id0]: porte A,k,30-30,DK/30-30
[id1]: porte B,k,10-10,DK/10-30

Re: array et foreach ligne par ligne

Posté : 15 avr. 2013, 10:33
par tom_nameless
J'ai glissé un var_dump(); et ça me donne ça :
Array ( [ref_porte] => Array ( [0] => Hall d'entrée 1 [1] => porte bureau DG [2] => porte bureau DG [3] => porte bureau DG [4] => porte bureau DG ) [longueur] => Array ( [0] => 30-30 [1] => 30-30 [2] => 30-30 [3] => 30-30 [4] => 30-30 ) [bouton] => Array ( [0] => K [1] => K [2] => K [3] => K [4] => K ) [ref_cylindre] => Array ( [0] => SK/30-30 [1] => SK/30-30 [2] => SK/30-30 [3] => SK/30-30 [4] => SK/30-30 ) )
Le tableau est-il bon maintenant ?

Je souhaite arriver à l'exemple ci-dessous :
Ligne 1 = Hall d'entrée 1,30-30,K,SK/30-30
Ligne 2 = porte bureau DG,30-30,K,SK/30-30

Re: array et foreach ligne par ligne

Posté : 15 avr. 2013, 11:20
par moogli
par exemple comme ça pour avoir un tableau
<?php
$tab = ['ref_porte' => [0 => 'Hall d\'entrée 1', 1 => 'porte bureau DG', 2 => 'porte bureau DG ', 3 => 'porte bureau DG ', 4 => 'porte bureau DG'], 'longueur' => [0 => '30-30', 1 => '30-30', 2 => '30-30', 3 => '30 - 30', 4 => '30 - 30'], 'bouton' => [0 => ' K ', 1 => 'K ', 2 => ' K ', 3 => 'K ', 4 => ' K'], 'ref_cylindre' => [0 => 'SK/30-30', 1 => 'SK/30-30', 2 => 'SK/30-30', 3 => 'SK/30-30', 4 => 'SK/30-30']];
echo '<table>
    <thead>
    <tr>
    <th>Ref porte</th>
    <th>Longueur</th>
    <th>bouton</th>
    <th>ref cylindre</th>
    </tr>
    </thead>
    <tbody>
';
foreach ($tab['ref_porte'] as $index => $value) {
    echo '<tr>
        <td>', $value, '</td>
        <td>', $tab['longueur'][$index], '</td>
        <td>', $tab['bouton'][$index], '</td>
        <td>', $tab['ref_cylindre'][$index], '</td>
        </tr>';

}
echo '</tbody>
</table>';
il faut que tu prenne du temps pour comprendre l'utilisation du foreach c'est assez simple et extrêmement utile pour l'utilisation des tableaux (ou objet).


@+