2011-03-09 3 views
0

J'ai un tableau et j'utilise print_r et ce qui se produit:Impression sur chaque élément de tableau

Array 
(
    [141] => 1 
    [171] => 3 
    [156] => 2 
    [241] => 1 
    [271] => 1 
    [256] => 1 
    [341] => 1 
    [371] => 1 
    [356] => 1 
    [441] => 1 
    [471] => 1 
) 

Comment puis-je imprimer l'index [141] et ainsi de suite?

+2

Vous pouvez regarder le manuel. La structure de contrôle 'foreach' en particulier http://au2.php.net/manual/fr/control-structures.foreach.php –

Répondre

1

Utilisez array_keys pour obtenir les clés d'un tableau associatif:

echo implode(', ', array_keys(array(141=>'a', 142=>'b'))); 
// prints: 141, 142 
7

Utilisez foreach pour obtenir

foreach($your_array as $key=>$value) { 
    echo 'index is '.$key.' and value is '.$value; 
} 
1

si vous connaissez déjà l'index du tableau:

$arrayIndex = 141; 
echo $yourarray[$arrayIndex]; 

ou boucle à travers le tableau comme ceci:

foreach ($yourarray as $arrayItem) { 
echo $arrayItem; 
} 

ou si vous avez besoin de trouver la clé tableau/index:

foreach ($yourarray as $arrayIndex=>$arrayItem) { 
echo $arrayIndex." - ". $arrayItem; 
} 
+0

Merci pour l'aide des gars – jayx412

Questions connexes