Maison  >  Article  >  développement back-end  >  fonction array() en PHP

fonction array() en PHP

PHPz
PHPzavant
2023-08-20 19:49:061409parcourir

fonction array() en PHP

La fonction array() en PHP crée un tableau.

Le tableau est de trois types en PHP.

  • Tableaux indexés − C'est un tableau avec un index numérique

  • Tableaux associatifs − C'est un tableau avec des clés nommées

  • Tableaux multidimensionnels − C'est un tableau qui a un ou plusieurs tableaux

Syntaxe

// array with numeric index i.e. Indexed arrays
array(value1,value2...);
// array with named keys i.e. associative arrays
array(key1 => value1, key2 => value2...)

Paramètres

  • valeur − Définissez la valeur.

  • key − Définissez la clé.

Retour

La fonction array() renvoie un tableau de paramètres.

Ce qui suit est un exemple de tableaux indexés.

Exemple

 Démo en direct

<?php
$products = array("Electronics","Clothing","Accessories", "Footwear");
$len = count($products);
for($i = 0;$i<$len;$i++) {
   echo $products[$i];
   echo "<br>";
}
?>

输出

Electronics
Clothing
Accessories
Footwear

Ce qui suit est un exemple de tableaux associatifs.

Exemple

 Démo en direct

<?php
$rank = array("Football"=>"1","Cricket"=>"2");
foreach($rank as $mykey=>$myvalue) {
   echo "Key = " . $mykey . ", Value = " . $myvalue;
   echo "<br>";
}
?>

输出

Key = Football, Value = 1
Key = Cricket, Value = 2

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer
Article précédent:Fonction PHP sqrt()Article suivant:Fonction PHP sqrt()