Bevezetés a tömbökbe a PHP-ben
A következő cikk, a tömbök a PHP-ben, áttekintést nyújt tömbök létrehozásáról a PHP-ben. Egy tömb hasonló adattípusok gyűjteménye. Egy tömb több értéket tárol egyetlen változóban. Miért van szükség tömbre, ha az érték tárolásához a változó is elvégezhető? A válasz azért van, mert korlátozott adatok értékeinek tárolása, például az 5-ös számok száma lehetséges, de amikor a szám növekszik, hogy mondjuk 100 vagy 200, 100 értéket kell tárolnunk 100 változóban, ami egy kicsit nehéz, ezért tömbben tároljuk. Ezért használnak tömböket.
Hogyan hozzunk létre tömböket a PHP-ben?
Szintaxis:variablename = array();
VAGYvariablename(i) = value;,
Ahol a változó neve az i változó neve, a kulcs, vagy az index értéke az elem értéke.
Példa tömb létrehozására
$colors = array(“Red”, ”Green”, ”Blue”);
A tömb hosszának kiszámításához a count kulcsszót használjuk.$length = count($colors); // output is 3
A tömb minden értékét a tömb elemének nevezzük. A tömbindex nullával kezdődik. És a tömb utolsó elemének indexe a tömb teljes hossza mínusz 1.
A fenti példában a vörös mutatója 0, a zöld értéke 1 és a kék értéke 2. Valójában könnyebbé válik a tömb elérése az index vagy a kulcs segítségével. Ahhoz, hogy egy tömb minden indexében megkapjuk az értéket, áthúzzuk az adott tömbön. A tömb hurkolásához foreach hurkot vagy hurkot használunk.
Hogyan működik a tömb a PHP-ben?
A foreach és a forrás hurkok a tömb átvezetésére használhatók. Minden tömb kezdő indexe 0-tól és így tovább:
Tömbök típusai a PHP-ben
Háromféle tömb létezik a PHP-ben, tanulmányozzuk részletesen az egyes tömb típusokat:
- Numerikus vagy indexelt tömb.
- Asszociatív tömb.
- Többdimenziós tömb.
1. Numerikus tömb
Ez a tömbtípus, ahol az index mindig egy szám, nem lehet karakterlánc. Bármilyen elem és bármilyen elem tárolására képes.
Szintaxis:variable name = array(“value1”, ”value2”, ”value3”, ”value4”)
Kód:
<_?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);
?>
Kimenet:
VAGY
A numerikus tömb deklarálásának másik módja a következő program. Ebben a programban az érték módosítását és kinyomtatását is látni fogjuk.
Kód:
<_?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);
?>
Kimenet:
Most megtanuljuk, hogyan lehet a for hurkot egy tömbön áthaladni
Kód:
<_?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 "
";
)
?>
Kimenet:
2. Asszociatív tömb
Ez a tömb kulcs-érték pár formájában van, ahol a kulcs a tömb indexe, és az érték a tömb eleme.
Szintaxis:
$input = array(“key1”=>”value1”,
“key2”=>”value2”,
“key3”=>”value3”,
“key4”=>”value4”);
VAGY
A másik módszer asszociatív tömb deklarálására tömb kulcsszó nélkül
$input($key1) = $value1;
$input($key2) = $value2;
$input($key3) = $value3;
$input($key4) = $value4;
Kód:
?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."
";)
?>
Kimenet:
3. Többdimenziós tömb
Ez a tömb egy tömb tömb, ahol a tömb értéke tömböt tartalmaz.
Szintaxis:
$input =array(
array('value1', 'value2', 'value3'),
array('value4', 'value5', 'value6'),
array('value7', 'value8', 'value9'));,
Kód:
<_?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 "
";
)
?>
Kimenet:
VAGY
Többdimenziós tömb asszociatív tömbben
Kód:
<_?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. "
";)
)?>
Kimenet:
Tömb módszer PHP-ben
Az alábbiakban bemutatjuk a tömb módszerét a PHP-ben:
1. gróf () módszer
Ez a módszer arra szolgál, hogy megszámolja a tömb elemeinek számát.
Szintaxis: A Count(array, mode) where the count is required mode is optional.
Kód:
<_?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);
?>
Kimenet:
3
2. Array_walk () módszer
Ez a módszer két paramétert vesz bemenetként, az első paraméter a bemeneti tömb, a második paraméter a deklarált funkció neve. Ezt a módszert a tömb minden elemének áthúzására használják.
Szintaxis:array_walk(array, function_name, parameter…)
where array is required, function_name is required
parameter is optional
Kód:
<_?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");
?>
Kimenet:
e – angol h –Hindi m –Marathi
3. In_array () módszer
Ez a módszer keresést végez a tömbön, függetlenül attól, hogy az adott tömb tartalmaz-e egy bizonyos értéket, vagy sem. Ha megtalálják vagy nem találják meg, akkor végrehajtják a megfelelő if blokkolást
Szintaxis:in_array(search_value, array_name)
Where both the parameters are required
Kód:
<_?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";
)
?>
Kimenet:
Talált matematika az adott tömbben
4. Array_pop () módszer
Ez a módszer eltávolítja az utolsó elemet az adott tömbből.
Szintaxis array_pop(array_name)
Kód:
<_?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);
?>
Kimenet:
5. Array_push () módszer
Ez a módszer hozzáadja a megadott elemeket a tömb végéhez.
Szintaxis:
array_push(array_name, value1, value2, …)
Kód:
<_?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);
?>
Kimenet:
6. Array_shift () módszer
Ez a módszer eltávolítja és visszatér a tömb első elemét.
Szintaxis: array_shift(array_name)
Kód:
<_?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);
?>
Kimenet:
7. Array_unshift () módszer
Ez a módszer megadott elemeket illeszt be a tömb elejére.
Szintaxis:
array_unshift(array_name, value1, value2, …)
Kód:
<_?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);
?>
Kimenet:
8. Array_reverse () módszer
Ezt a módszert a tömb elemeinek megfordítására használják.
Szintaxis:array_reverse(array_name, preserve)
where array_name is required,
preserve is optional
Kód:
<_?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));
?>
Kimenet:
Következtetés
Ez a cikk a PHP egyszerű és összetett fogalmainak minden szintjét lefedi. Remélem, hogy ez a cikk érdekes és informatívnak találta a tanulási célokat.
Ajánlott cikkek
Ez a PHP tömbjeinek útmutatója volt. Itt tárgyaljuk, hogyan lehet tömböket létrehozni a PHP-ben ?, Hogyan működik a tömb a PHP-ben? Megnézheti a többi javasolt cikket is, hogy többet megtudjon-
- Tömbök R-ben
- Mi a PHP?
- A PHP előnyei
- Bevezetés a PHP-be
- Különböző típusú hurkok és azok előnyei
- Többdimenziós tömb a PHP-ben