Skip to content

Instantly share code, notes, and snippets.

@loschke
Last active January 17, 2023 13:28
Show Gist options
  • Save loschke/eca47ee1d75a3cb62a1854bfb523add5 to your computer and use it in GitHub Desktop.
Save loschke/eca47ee1d75a3cb62a1854bfb523add5 to your computer and use it in GitHub Desktop.
<?php
$zahlen = array(11,22,33,44);
$laender = array("England", "Deutschland", "Kanada", "Schweiz");
?>
<?php
print_r($zahlen); //Array ausgeben
Array ( [0] => 11 [1] => 22 [2] => 33 [3] => 44 ) //Ergebnis
print_r($laender); //Array ausgeben
Array ( [0] => England [1] => Deutschland [2] => Kanada [3] => Schweiz ) //Ergebnis
?>
<?php
$laender = array("EN"=>"England","DE"=>"Deutschland","CA"=>"Kanada","CH"=>"Schweiz");
print_r($laender); //Ausgabe
Array ( [EN] => England [DE] => Deutschland [CA] => Kanada [CH] => Schweiz )
$germany = array(
"Capital" => "Berlin",
"Currency" => "Euro",
"Continent" => "Europoa",
"Language" => "Deutsch",
"Domain" => ".de"
);
print_r($germany); //Ausgabe
Array ( [Capital] => Berlin [Currency] => Euro [Continent] => Europoa [Language] => Deutsch [Domain] => .de )
?>
<?php
//zweidimensionaler Array
$laender = array(
"DE"=> "Deutschland",
"GB"=> array("England","Schottland","Wales","Nordirland"),
"CA"=> "Kanada"
);
//dreidimensionaler Array
$laenderinfo = array(
"EN" => array(
"Country"=> "England",
"Capital"=> "London",
"Currency"=> "Pfund",
"Continent"=> "Europoa",
"Language"=> "englisch",
"Domain"=> ".uk"
),
"DE" => array(
"Country"=> "Deutschland",
"Capital"=> "Berlin",
"Currency"=> "Euro",
"Continent"=> "Europoa",
"Language"=> "deutsch",
"Domain"=> ".de"
),
"CA" => array(
"Country"=> "Kanady",
"Capital"=> "Ottawa",
"Currency"=> "Kanad. Dollar",
"Continent"=> "Nordamerika",
"Language"=> array ("englisch", "französisch"),
"Domain"=> ".ca"
)
);
?>
<?php
$laender = array("England", "Deutschland", "Kanada", "Schweiz");
echo $laender[0]; //ergibt: England
echo $laender[2]; //ergibt: Kanada
//assoziativer Array
$laender = array(
"EN" => "England",
"DE" => "Deutschland",
"CA" => "Kanada",
"CH" => "Schweiz"
);
echo $laender["DE"]; // ergibt: Deutschland
echo $laender["EN"]; // ergibt: England
?>
<?php
//zweidimensionaler Array
$laender = array(
"DE"=> "Deutschland",
"GB"=> array("England","Schottland","Wales","Nordirland"),
"CA"=> "Kanada"
);
echo $lander{"DE"]; //ergibt: Deutschland
echo $laender["GB"][0]; //ergibt: England
//dreidimensionaler Array
$laenderinfo = array(
"EN" => array(
"Country"=> "England",
"Capital"=> "London",
"Currency"=> "Pfund",
"Continent"=> "Europoa",
"Language"=> "englisch",
"Domain"=> ".uk"
),
"DE" => array(
"Country"=> "Deutschland",
"Capital"=> "Berlin",
"Currency"=> "Euro",
"Continent"=> "Europoa",
"Language"=> "deutsch",
"Domain"=> ".de"
),
"CA" => array(
"Country"=> "Kanady",
"Capital"=> "Ottawa",
"Currency"=> "Kanad. Dollar",
"Continent"=> "Nordamerika",
"Language"=> array ("englisch", "französisch"),
"Domain"=> ".ca"
)
);
echo $laenderinfo["EN"]["Capital"]; //ergibt: London
echo $laenderinfo["CA"]["Language"][0]; //ergibt: englisch
?>
<?php
//Eindimensionale Arrays
//einfacher Array
$laender = array("England", "Deutschland", "Kanada", "Schweiz");
foreach ($laender as $land) {
echo $land . ", ";
}
//ergibt: England, Deutschland, Kanada, Schweiz,
//Assoziativer Array
$laender2 = array(
"EN" => "England",
"DE" => "Deutschland",
"CA" => "Kanada",
"CH" => "Schweiz"
);
foreach ($laender2 as $key => $land) {
echo $key . " - " . $land . "<br>";
}
/* ergibt:
EN - England
DE - Deutschland
CA - Kanada
CH - Schweiz
*/
// Multidimensionaler Array
//zweidimensional - verschachtelung der Schleifen
$laender3 = array(
"DE" => "Deutschland",
"GB" => array("England","Schottland","Wales","Nordirland"),
"CA" => "Kanada",
"CH" => "Schweiz"
);
foreach ($laender3 as $key => $land) {
if(is_array($land)) {
echo $key . " - ";
foreach ($land as $details) {
echo $details . ", ";
}
echo "<br>";
} else {
echo $key . " - " . $land . "<br>";
}
}
/* ergibt:
DE - Deutschland
GB - England, Schottland, Wales, Nordirland,
CA - Kanada
CH - Schweiz
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment