Introduktion til Arrays i PHP

Den følgende artikel, Arrays i PHP, giver dig en oversigt over oprettelsen af ​​arrays i PHP. En matrix er en samling af lignende datatyper. En matrix gemmer flere værdier i en enkelt variabel. Hvorfor er der behov for en matrix, når arbejdet med at gemme en værdi også kan udføres med variabel? Svaret skyldes, at det er muligt at gemme værdier for begrænsede data som antallet af numre 5, men når antallet stiger til at sige 100 eller 200, er vi nødt til at gemme 100 værdier i 100 variabler, hvilket er en smule vanskeligt, så vi gemmer det i en matrix. Derfor bruges arrays.

Sådan oprettes arrays i PHP?

Syntaks:
variablename = array();
ELLER
variablename(i) = value;,

Hvor variabelnavn er variablen i er nøglen, eller indeksværdien er elementværdien.

Eksempel til oprettelse af en matrix

$colors = array(“Red”, ”Green”, ”Blue”);
For at beregne længden på array bruger vi tælleordet.
$length = count($colors); // output is 3

Hver værdi i arrayet kaldes et element i matrixen. Array-indekset begynder med 0. Og indekset for det sidste element i en matrix er den samlede længde af arrayen minus 1.

I det givne eksempel ovenfor er indekset for Rødt 0, Grønt er 1 og det for Blå er 2. Det bliver virkelig lettere at få adgang til matrixen ved hjælp af indekset eller en nøgle. For at få værdien ved hvert indeks i en matrix går vi gennem det givne array. For at sløjfe arrayet bruger vi en foreach-loop eller til en loop.

Hvordan fungerer Array i PHP?

Sløjfer som foreach og for bruges til at gå gennem arrayet. Hver række har startindekser fra 0 og så videre:

Typer af arrays i PHP

Der er 3 typer array i PHP, lad os lære hver type array i detaljer:

  1. Numerisk eller indekseret matrix.
  2. Associative Array.
  3. Multidimensionel matrix.

1. Numerisk matrix

Denne type array, hvor et indeks altid er et tal, kan ikke være en streng. Det kan gemme et vilkårligt antal elementer og også enhver type elementer.

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

Kode:

<_?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);
?>

Produktion:

ELLER

Den anden måde at erklære det numeriske array på er som det følgende program. I dette program vil vi også se at ændre og udskrive værdi.

Kode:

<_?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);
?>

Produktion:

Nu lærer vi, hvordan man bruger for-loop til at krydse gennem en matrix

Kode:

<_?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 "
";
)
?>

Produktion:

2. Associative Array

Denne matrix er i form af et nøgleværdipar, hvor nøglen er indekset for matrixen, og værdien er elementet i arrayen.

Syntaks:

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

ELLER

Den anden måde at erklære et tilknyttet array uden nøgleord for array

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

Kode:

?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."
";)
?>

Produktion:

3. Multidimensionel matrix

Denne matrix er en matrix med array, hvor værdien af ​​array indeholder en matrix.

Syntaks:

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

Kode:

<_?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 "
";
)
?>

Produktion:

ELLER

Multidimensionel matrix i et associerende array

Kode:

<_?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. "
";)
)?>

Produktion:

Metoder til array i PHP

Herunder er metoderne til Array i PHP:

1. Count () metode

Denne metode bruges til at tælle antallet af elementer i en matrix.

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

Kode:

<_?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);
?>

Produktion:

3

2. Metode Array_walk ()

Denne metode tager to parametre som input, den første parameter er input array, den anden parameter er navnet på den erklærede funktion. Denne metode bruges til at loop gennem hvert element i matrixen.

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

Kode:

<_?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");
?>

Produktion:

e – engelsk h –Hindi m –Marathi

3. Metoden In_array ()

Denne metode udfører en søgning på matrixen, uanset om den givne matrix indeholder en bestemt værdi eller ej. Hvis den findes eller ikke findes, udfører den respektive hvis, ellers blokering

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

Kode:
<_?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";
)
?>

Produktion:

Fundet matematik i det givne array

4. Array_pop () -metode

Denne metode fjerner det sidste element fra det givne array.

Syntaks array_pop(array_name)

Kode:

<_?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);
?>

Produktion:

5. Array_push () -metoden

Denne metode tilføjer givne elementer i slutningen af ​​matrixen.

Syntaks:

array_push(array_name, value1, value2, …)

Kode:
<_?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);
?>

Produktion:

6. Metode Array_shift ()

Denne metode fjerner og returnerer det første element i matrixen.

Syntaks: array_shift(array_name)

Kode:

<_?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);
?>

Produktion:

7. Array_unshift () -metode

Denne metode indsætter givne elementer til starten af ​​matrixen.

Syntaks:

array_unshift(array_name, value1, value2, …)

Kode:

<_?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);
?>

Produktion:

8. Array_reverse () -metode

Denne metode bruges til at vende elementerne i arrayet.

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

Kode:
<_?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));
?>

Produktion:

Konklusion

Denne artikel dækker alle niveauer af koncepter enkle og komplekse af emnearrays i PHP. Håber du fandt, at denne artikel var interessant og informativ til læringsformålet.

Anbefalede artikler

Dette har været en guide til Arrays i PHP. Her diskuterer vi, hvordan man opretter arrays i PHP?, Hvordan fungerer array i PHP?, 3 typer og 8 metoder til array i PHP med passende syntaks, kode og output. Du kan også gennemgå vores andre foreslåede artikler for at lære mere-

  1. Arrays i R
  2. Hvad er PHP?
  3. Fordele ved PHP
  4. Introduktion til PHP
  5. Forskellige typer loops med dens fordele
  6. Multidimensionel matrix i PHP

Kategori: