Introduksjon til Arrays i PHP

Følgende artikkel, Arrays in PHP, gir deg en oversikt for oppretting av matriser i PHP. En matrise er en samling lignende datatyper. En matrise lagrer flere verdier i en enkelt variabel. Hvorfor er det behov for en matrise når arbeidet med å lagre en verdi også kan utføres med variabel? Svaret er fordi det er mulig å lagre verdier på begrensede data som antall 5, men når tellingen øker til å si 100 eller 200, må vi lagre 100 verdier i 100 variabler, noe som er litt vanskelig, så vi lagrer det i en matrise. Dette er grunnen til at matriser brukes.

Hvordan lage matriser i PHP?

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

Der variabelenavn er navnet på variabelen i er nøkkelen eller indeksverdien er elementverdien.

Eksempel for å opprette en matrise

$colors = array(“Red”, ”Green”, ”Blue”);
For å beregne lengden på array bruker vi telletastordet.
$length = count($colors); // output is 3

Hver verdi i matrisen betegnes som et element i matrisen. Arrayindeksen begynner med 0. Og indeksen for det siste elementet i en matrise er den totale lengden på arrayen minus 1.

I det gitte eksemplet over er indeksen på Rødt 0, Grønn er 1 og den for Blå er 2. Det blir virkelig lettere å få tilgang til matrisen ved hjelp av indeksen eller en nøkkel. For å få verdien ved hver indeks i en matrise slenger vi gjennom den gitte matrisen. For å sløyfe matrisen bruker vi en foreach-loop eller for en loop.

Hvordan fungerer Array i PHP?

Looper som foreach og for brukes til å sløyfe gjennom matrisen. Hver gruppe har startindekser fra 0 og så videre:

Typer Arrays i PHP

Det er tre typer array i PHP, la oss lære hver type array i detalj:

  1. Numerisk eller indeksert matrise.
  2. Associative Array.
  3. Flerdimensjonal matrise.

1. Numerisk matrise

Denne typen matriser der en indeks alltid er et tall, kan ikke være en streng. Den kan lagre et hvilket som helst antall elementer og også alle typer 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);
?>

Produksjon:

ELLER

Den andre måten å deklarere den numeriske matrisen er i henhold til følgende program. I dette programmet vil vi også se på å endre og skrive ut verdi.

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

Produksjon:

Nå skal vi lære hvordan du bruker for-loopen til å krysse gjennom en matrise

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

Produksjon:

2. Associative Array

Denne matrisen er i form av et nøkkelverdipar, der nøkkelen er indeksen til arrayen og verdien er elementet i arrayen.

syntaks:

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

ELLER

Den andre måten å erklære et assosiativt utvalg uten nøkkelord

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

Produksjon:

3. Flerdimensjonal matrise

Denne matrisen er en matrise der verdien av matrisen inneholder en matrise.

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

Produksjon:

ELLER

Flerdimensjonal matrise i en assosiativ matrise

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

Produksjon:

Methods of Array i PHP

Nedenfor er metodene til Array i PHP:

1. Count () -metode

Denne metoden brukes til å telle antall elementer i en matrise.

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

Produksjon:

3

2. Array_walk () -metoden

Denne metoden tar to parametere som input, den første parameteren er input-arrayen, den andre parameteren er navnet på den deklarerte funksjonen. Denne metoden brukes til å sløyfe gjennom hvert element i matrisen.

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

Produksjon:

e – engelsk h –Hindi m –Marathi

3. In_array () -metoden

Denne metoden utfører et søk på matrisen, enten det gitte arrayet inneholder en bestemt verdi eller ikke. Hvis funnet eller ikke funnet, vil den utføre respektive hvis, ellers blokkerer

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

Produksjon:

Fant matematikk i det angitte arrayet

4. Array_pop () -metoden

Denne metoden fjerner det siste elementet fra den gitte matrisen.

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

Produksjon:

5. Array_push () -metoden

Denne metoden legger til gitte elementer på slutten av matrisen.

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

Produksjon:

6. Array_shift () -metoden

Denne metoden fjerner og returnerer det første elementet i matrisen.

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

Produksjon:

7. Array_unshift () -metoden

Denne metoden setter inn gitte elementer til begynnelsen av matrisen.

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

Produksjon:

8. Array_reverse () -metode

Denne metoden brukes til å reversere elementene i matrisen.

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

Produksjon:

Konklusjon

Denne artikkelen dekker alle nivåer av konsepter enkle og sammensatte av emnearriser i PHP. Håper du fant denne artikkelen som interessant og lærerik for læringsformålet.

Anbefalte artikler

Dette har vært en guide til Arrays i PHP. Her diskuterer vi hvordan å lage matriser i PHP ?, Hvordan fungerer array i PHP?, 3 typer og 8 metoder for array i PHP med passende syntaks, kode og utdata. Du kan også gå gjennom andre foreslåtte artikler for å lære mer-

  1. Arrays i R
  2. Hva er PHP?
  3. Fordeler med PHP
  4. Introduksjon til PHP
  5. Ulike typer løkker med fordelene
  6. Multidimensjonal matrise i PHP