Einführung in Arrays in PHP

Der folgende Artikel, Arrays in PHP, enthält eine Übersicht über die Erstellung von Arrays in PHP. Ein Array ist eine Sammlung ähnlicher Datentypen. Ein Array speichert mehrere Werte in einer einzelnen Variablen. Warum wird ein Array benötigt, wenn die Speicherung eines Werts auch durch eine Variable erfolgen kann? Die Antwort ist, weil das Speichern von Werten mit begrenzten Daten wie Anzahl von Zahlen 5 möglich ist, aber wenn die Anzahl auf 100 oder 200 ansteigt, müssen wir 100 Werte in 100 Variablen speichern, was ein bisschen schwierig ist, also speichern wir es in einem Array. Aus diesem Grund werden Arrays verwendet.

Wie erstelle ich Arrays in PHP?

Syntax:
variablename = array();
ODER
variablename(i) = value;,

Wobei Variablenname der Name der Variablen ist, ist i der Schlüssel oder der Indexwert ist der Elementwert.

Beispiel zum Erstellen eines Arrays

$colors = array(“Red”, ”Green”, ”Blue”);
Um die Länge des Arrays zu berechnen, verwenden wir das Schlüsselwort count.
$length = count($colors); // output is 3

Jeder Wert im Array wird als Element des Arrays bezeichnet. Der Array-Index beginnt mit 0. Und der Index des letzten Elements in einem Array ist die Gesamtlänge des Arrays minus 1.

Im obigen Beispiel ist der Index für Rot 0, für Grün 1 und für Blau 2. Mit Hilfe des Index oder eines Schlüssels wird der Zugriff auf das Array wirklich einfacher. Um den Wert an jedem Index eines Arrays zu erhalten, durchlaufen wir das angegebene Array. Zum Schleifen des Arrays verwenden wir eine foreach-Schleife oder eine for-Schleife.

Wie funktioniert das Array in PHP?

Schleifen wie foreach und for werden zum Durchlaufen des Arrays verwendet. Jedes Array hat Startindizes von 0 und so weiter:

Arten von Arrays in PHP

In PHP gibt es drei Arten von Arrays:

  1. Numerisches oder indiziertes Array.
  2. Assoziatives Array.
  3. Mehrdimensionales Array.

1. Numerisches Array

Dieser Array-Typ, bei dem ein Index immer eine Zahl ist, kann kein String sein. Es können beliebig viele Elemente und auch beliebige Arten von Elementen gespeichert werden.

Syntax:
variable name = array(“value1”, ”value2”, ”value3”, ”value4”)

Code:

<_?php
//Example to demonstrate numeric array
$input = array("Apple", "Orange", "Banana", "Kiwi");
//Here, to get these values we will write like
echo $input(0) . "\n"; // will give Apple
echo $input(1) . "\n"; // will give Orange
echo $input(2) . "\n"; // will give Banana
echo $input(3) . "\n"; // will give Kiwi
// To get the length of array we will use count
echo "The count of the array is " . count($input); // will give 4
echo "\n";
//To print the array we can use
print_r($input);
?>

Ausgabe:

ODER

Die andere Möglichkeit, das numerische Array zu deklarieren, ist das folgende Programm. In diesem Programm werden wir auch sehen, Wert zu ändern und zu drucken.

Code:

<_?php
//Example to demonstrate numeric array in another way
$input(0) = "Apple";
$input(1) = "Orange";
$input(2) = "Banana";
$input(3) = "Kiwi";
// To get Kiwi we will write like
echo $input(3)."
"; // will give Kiwi
//To modify Orange value
$input(1) = "Mango";
// Now echo $input(1) will give Mango
echo $input(1)."
"; // Mango
//To print the array we can use
print_r($input);
?>

Ausgabe:

Jetzt lernen wir, wie man die for-Schleife verwendet, um ein Array zu durchlaufen

Code:

<_?php
//Example to demonstrate for loop on a numeric array
//declaring the array
$input = array("Apple", "Orange", "Banana", "Kiwi", "Mango");
//the for loop to traverse through the input array
for($i=0;$i echo $input($i);
echo "
";
)
?>
//Example to demonstrate for loop on a numeric array
//declaring the array
$input = array("Apple", "Orange", "Banana", "Kiwi", "Mango");
//the for loop to traverse through the input array
for($i=0;$i echo $input($i);
echo "
";
)
?>

Ausgabe:

2. Assoziatives Array

Dieses Array hat die Form eines Schlüssel-Wert-Paares, wobei der Schlüssel der Index des Arrays und der Wert das Element des Arrays ist.

Syntax:

$input = array(“key1”=>”value1”,
“key2”=>”value2”,
“key3”=>”value3”,
“key4”=>”value4”);

ODER

Die andere Möglichkeit, ein assoziatives Array ohne Array-Schlüsselwort zu deklarieren

$input($key1) = $value1;
$input($key2) = $value2;
$input($key3) = $value3;
$input($key4) = $value4;

Code:

?php
//Example to demonstrate associative array
//declaring an array
$input = array(
"Jan"=>31,
"Feb"=>28,
"Mar"=>31,
"Apr"=>30);
// the for loop to traverse through the input array
foreach($input as $in) (
echo $in."
";)
?>

Ausgabe:

3. Mehrdimensionales Array

Dieses Array ist ein Array von Array, wobei der Wert von Array ein Array enthält.

Syntax:

$input =array(
array('value1', 'value2', 'value3'),
array('value4', 'value5', 'value6'),
array('value7', 'value8', 'value9'));,

Code:

<_?php
//Example to demonstrate multidimensional array
// declaring a multidimensional array
$input = array ("colors"=>array ("Red", "Green", "Blue"),
"fruits"=>array ("Apple", "Orange", "Grapes"),
"cars"=>array ("Skoda", "BMW", "Mercedes")
);
//the foreach loop to traverse through the input array
foreach($input as $key=>$value) (
echo $key .'--'. "
";
foreach($value as $k=>$v)
(echo $v ." ";)
echo "
";
)
?>

Ausgabe:

ODER

Mehrdimensionales Array in einem assoziativen Array

Code:

<_?php
//Example to demonstrate multidimensional array
// declaring a multidimensional array
$input = array(
"The_Alchemist" => array (
"author" => "Paulo Coelho",
"type" => "Fiction",
"published_year" => 1988),
"Managing_Oneself" => array(
"author" => "Peter Drucker",
"type" => "Non-fiction",
"published_year" => 1999
), "Measuring_the_World" => array(
"author" => "Daniel Kehlmann",
"type" => "Fiction",
"published_year" => 2005
));
//the foreach loop to traverse through the input array
//foreach to loop the outer array
foreach($input as $book) (
echo "
";
// foreach to loop the inner array
foreach($book as $key=>$value)
(
echo $key." ". $value. "
";)
)?>

Ausgabe:

Array-Methoden in PHP

Nachfolgend sind die Methoden von Array in PHP aufgeführt:

1. Count () Methode

Mit dieser Methode wird die Anzahl der Elemente in einem Array gezählt.

Syntax: Count(array, mode) where the count is required mode is optional.

Code:

<_?php
//Example to demonstrate use of in_array method
//declaring associative array
$input=array('English', 'Hindi', 'Marathi');
//counting the number of elements in the given array
echo count($input);
?>

Ausgabe:

3

2. Array_walk () -Methode

Diese Methode verwendet zwei Parameter als Eingabe, der erste Parameter ist das Eingabearray, der zweite Parameter ist der Name der deklarierten Funktion. Mit dieser Methode werden die einzelnen Elemente im Array durchlaufen.

Syntax :
array_walk(array, function_name, parameter…)
where array is required, function_name is required
parameter is optional

Code:

<_?php
//Example to demonstrate use of array_walk method
//creating a function to print the key and values of the given array
function fun($val, $k) (
echo $k. " --" .$val ."\n";
)
// declaring associative array
$input=array("e"=>'English', "h"=>'Hindi', "m"=>'Marathi');
//passing this array as a first parameter to the function
// array_walk,
//second paramter as the name of the function being called
array_walk($input, "fun");
?>

Ausgabe:

e – Englisch h – Hindi m – Marathi

3. In_array () -Methode

Diese Methode führt eine Suche im Array durch, unabhängig davon, ob das angegebene Array einen bestimmten Wert enthält oder nicht. Wenn gefunden oder nicht gefunden, wird der entsprechende if, else-Block ausgeführt

Syntax:
in_array(search_value, array_name)
Where both the parameters are required

Code:
<_?php
//Example to demonstrate use of in_array method
// declaring associative array
$input=array('English', 'Hindi', 'Marathi', "Maths", "Social Science");
// using in_array to find Maths in given array
if(in_array("Maths", $input)) (
echo "Found Maths in the given array";
)
else
(
echo "Did not find Maths in the given array";
)
?>

Ausgabe:

Gefundene Mathematik in dem angegebenen Array

4. Array_pop () -Methode

Diese Methode entfernt das letzte Element aus dem angegebenen Array.

Syntax array_pop(array_name)

Code:

<_?php
//Example to demonstrate use of array_pop method
// declaring array
$input=array('English', 'Hindi', 'Marathi');
// before using array_pop on the given array
print_r($input);
// after using array_pop method on the given array
array_pop($input);
echo "\n ";
print_r($input);
?>

Ausgabe:

5. Array_push () -Methode

Diese Methode fügt bestimmte Elemente am Ende des Arrays hinzu.

Syntax:

array_push(array_name, value1, value2, …)

Code:
<_?php
//Example to demonstrate use of array_push method
// declaring array
$input=array('English', 'Hindi', 'Marathi');
// before using array_push on the given array
print_r($input);
// after using array_push method on the given array
array_push($input, "Economics", "Maths", "Social Science");
echo "\n";
//printing the array
print_r($input);
?>

Ausgabe:

6. Array_shift () -Methode

Diese Methode entfernt das erste Element des Arrays und gibt es zurück.

Syntax: array_shift(array_name)

Code:

<_?php
//Example to demonstrate use of array_push method
// declaring array
$input=array('English', 'Hindi', 'Marathi');
// before using array_shift on the given array
print_r($input);
echo "\n";
// after using array_shift method on the given array
echo array_shift($input);
?>

Ausgabe:

7. Array_unshift () -Methode

Diese Methode fügt bestimmte Elemente an den Anfang des Arrays ein.

Syntax:

array_unshift(array_name, value1, value2, …)

Code:

<_?php
//Example to demonstrate use of array_push method
// declaring array
$input=array('English', 'Hindi', 'Marathi');
// before using array_unshift on the given arrayprint_r($input);
echo "\n";
// after using array_unshift method on the given array
array_unshift($input, "Economics");
print_r($input);
?>

Ausgabe:

8. Array_reverse () Methode

Diese Methode wird verwendet, um die Elemente des Arrays umzukehren.

Syntax:
array_reverse(array_name, preserve)
where array_name is required,
preserve is optional

Code:
<_?php
//Example to demonstrate use of in_array method
// declaring associative array
$input=array("e"=>'English', "h"=>'Hindi', "m"=>'Marathi');
// array before reversing the elements
print_r($input);
echo "\n";
// printing the reverse
// array after reversing the elements
print_r(array_reverse($input));
?>

Ausgabe:

Fazit

Dieser Artikel behandelt alle Ebenen von Konzepten, die für die Themenfelder in PHP einfach und komplex sind. Ich hoffe, Sie fanden diesen Artikel interessant und informativ für den Lernzweck.

Empfohlene Artikel

Dies war eine Anleitung zu Arrays in PHP. Hier besprechen wir, wie Arrays in PHP erstellt werden ?, Wie funktionieren Arrays in PHP ?, 3 Arten und 8 Methoden von Arrays in PHP mit entsprechender Syntax, Code und Ausgabe. Sie können auch unsere anderen Artikelvorschläge durchgehen, um mehr zu erfahren.

  1. Arrays in R
  2. Was ist PHP?
  3. Vorteile von PHP
  4. Einführung in PHP
  5. Verschiedene Arten von Schleifen mit ihren Vorteilen
  6. Mehrdimensionales Array in PHP