Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision Next revision Both sides next revision | ||
amprog_05 [2016-10-07 08:32] Anders Mosshäll created |
amprog_05 [2016-10-07 08:38] Anders Mosshäll |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== Anders Genomgång 05 - Arrayer ====== | ====== Anders Genomgång 05 - Arrayer ====== | ||
+ | |||
+ | PowerPoint om arrayer: {{:: | ||
<php> | <php> | ||
Line 15: | Line 17: | ||
// Lägga in data i array | // Lägga in data i array | ||
// Skriva ut ett värde på en speciell position | // Skriva ut ett värde på en speciell position | ||
+ | // Array i en array | ||
include(' | include(' | ||
- | + | ||
- | $names = array ('Astrid', 'Birgitta', 'Cecilia'); | + | $names = array ('Anders', 'Lisa', 'Micke'); |
echo $names; | echo $names; | ||
echo '< | echo '< | ||
print_r($names); | print_r($names); | ||
echo $names[1] . '< | echo $names[1] . '< | ||
+ | |||
$i=0; | $i=0; | ||
while ($i < 3) { // Använd en loop för att gå igenom allt innehåll. | while ($i < 3) { // Använd en loop för att gå igenom allt innehåll. | ||
Line 28: | Line 31: | ||
$i++; | $i++; | ||
} | } | ||
+ | |||
$i=0; | $i=0; | ||
while ($i < count($names)) { // count() tar reda på hur många det finns i en array | while ($i < count($names)) { // count() tar reda på hur många det finns i en array | ||
Line 34: | Line 37: | ||
$i++; | $i++; | ||
} | } | ||
- | + | ||
- | $i=3; | + | $i=1; |
- | while ($i < 11) { // Loop kan användas för att sätta värden. | + | while ($i < 11) { // Loop kan användas för att sätta värden. |
$arr1[$i] = $i*5; | $arr1[$i] = $i*5; | ||
$i++; | $i++; | ||
} | } | ||
print_r($arr1); | print_r($arr1); | ||
+ | |||
+ | echo '< | ||
$j=0; | $j=0; | ||
Line 49: | Line 54: | ||
} | } | ||
print_r($arr2); | print_r($arr2); | ||
+ | |||
echo '< | echo '< | ||
- | //$ages = array (34, | + | $ages2 = array ( // Strängar som nycklar |
- | $ages = array ( // Kan byta till andra nycklar | + | 'Anders' => 29, |
- | 'Astrid' => 34, // Lättare att se om man delar upp på flera rader. | + | ' |
- | 'Birgitta' => 23, | + | 'Micke' => 39, |
- | 'Cecilia' => 45 | + | 'Ella' => 2 |
); | ); | ||
- | print_r($ages); | + | print_r($ages2); |
- | echo $ages['Birgitta'] . '< | + | echo $ages2['Lisa'] . '< |
- | + | ||
- | $arr3 = array ( // Kan innehålla flera olika saker. | + | $info = array ( // Kan innehålla flera olika saker. |
- | ' | + | ' |
- | ' | + | ' |
- | 3 => 56, | + | ' |
- | ' | + | |
); | ); | ||
- | print_r($arr3); | + | print_r($info); |
- | echo $arr3['colors' | + | echo $info['favorite_colors' |
+ | |||
include(' | include(' | ||
+ | ?> | ||
</ | </ | ||