php array types
PHP ARRAY TYPES
PHP ARRAY
1]Array is a special type of variable that we use to store or store multiple values in a single variable without creating multiple variables to store these values.
2] We use the array function ( ) to create an array in PHP.
PHP ARRAY TYPES :-
1] Indexed Array
1] PHP index array is an array represented by an index number.
2] All elements of an array are represented by index numbers starting from 0.
3] PHP index arrays can store numbers, strings, or single objects.
4] PHP indexed arrays are also called numeric arrays.
SYNTAX ::-
foreach($array as $value)
{
statement;
statement;
statement;
}
PHP Array Type
Example :-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>array</title>
</head>
<body>
<?php
$student=array("vitthal","seeta","radha","ram");
rsort($student);
foreach($student as $a)
{
echo $a."<br>";
}
?>
</body>
</html>
PHP Array Type
Output
vitthal
seeta
ram
radha
2] Associative Array
1] PHP allows you to associate a name/tag with any string in PHP using the => symbol.
2] This way you can easily remember because each element is represented by a label instead of an additional number.
SYNTAX ::-
foreach ($array as $key => $value)
{
statement;
statement;
}
Associative Array
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>array</title>
</head>
<body>
<?php
$student=array(3=>"vitthal",1=>"seeta",2=>"radha");
foreach($student as $x=>$y)
{
echo $y."your Roll no is ".$x."<br> ";
}
?>
</body>
</html>
Associative Array
Output ::-
vitthalyour Roll no is 3
seetayour Roll no is 1
radhayour Roll no is 2
3] Multidimension Array
1] PHP multidimensional arrays are also called arrays.
2] It allows you to store table data in an array.
3] In PHP, multidimensional arrays can be represented as a row*column matrix.
Multidimension Array ::-
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>array</title>
</head>
<body>
<?php
$COMPANY = array (
array("TCS",22,18),
array("IBM",15,13),
array("INFOSYS",5,2),
);
echo $COMPANY[0][0].": In stock: ".$COMPANY[0][1].", sold: ".$COMPANY[0][2].".<br>";
echo $COMPANY[1][0].": In stock: ".$COMPANY[1][1].", sold: ".$COMPANY[1][2].".<br>";
echo $COMPANY[2][0].": In stock: ".$COMPANY[2][1].", sold: ".$COMPANY[2][2].".<br>";
?>
</body>
</html>
Post a Comment