array
Erstellen Sie ein neues Array
array()
wird verwendet, um ein Array zu erstellen.
In PHP gibt es drei Arten von Arrays:
Erstellen Sie ein Indexarray mit dem Namen $ Cars, weisen Sie ihm drei Elemente zu und drucken Sie den Text mit dem Array -Wert aus:
<? Php $ cars = Array ( "Volvo" , "BMW" , "Toyota" ) ; Echo "ich mag" . $ Cars [ 0 ] . "," . $ Cars [ 1 ] . " Und " . $ Cars [ 2 ] . "." ; ?>
Probieren Sie es selbst aus
Erstellen Sie ein assoziatives Array mit dem Namen $ Age:
<? Php $ Alter = Array ( "Bill" => "60" , "Steve" => "56" , "Mark" => "31" ) ; Echo "Bill ist" . $ ay ' bill' ] . "Jahre alt." ; ?>
Probieren Sie es selbst aus
Den Wert des Indexarrays iterieren und ausdrucken:
<? Php $ cars = Array ( "Volvo" , "BMW" , "Toyota" ) ; $ arrlength = count ( $ cars ) ; für ( $ x = 0 ; $ x < $ arrlength ; $ x ++ ) { Echo $ Cars [ $ x ] ; echo "<br>" ; } ?>
Probieren Sie es selbst aus
Alle Werte des assoziativen Arrays iterieren und ausdrucken:
<? Php $ Alter = Array ( "Bill" => "60" , "Steve" => "56" , "Mark" => "31" ) ; foreach ( $ älter als $ x => $ x_Value ) { echo "key =" . $ x . ", Value =" . $ x_Value ; echo "<br>" ; } ?>
Probieren Sie es selbst aus
Erstellen Sie ein mehrdimensionales Array:
<? Php // zweidimensionales Array: $ cars = Array ( Array ( "Volvo" , 100 , 96 ) , Array ( "BMW" , 60 , 59 ) , Array ( "Toyota" , 110 , 100 ) ) ; ?>
Probieren Sie es selbst aus