How to create array in PHP
Array is used to stores multiple values in one single variable
We can create array by using "array" Keyword
Type of Array
- Indexed arrays - Numeric index
- Associative arrays -Named keys
- Multidimensional arrays - Contain one or more arrays
Indexed Array Examples
Example 1
$color=array("red","Green","Blue");
//echo $color[1];
foreach($color as $val){
echo $val."
";
}
Example 2
$color=array("red","Green","Blue");
//echo $color[1];
foreach($color as $val){
echo $val."
";
}
Example 3
$array[0]="Shubham";
$array[1]="Rafiya";
foreach($array as $val){
echo $val."
";
}
echo count($val);
Example 4
$col=array("abc","ccv","dsf");
echo count($col);
Example 5
$col=array("abc","ccv","dsf");
$arrlen=count($col);
for($i=0;$i<$arrlen;$i++){
echo $col[$i]."
";
}
Example 6
$col=array("abc","ccv","dsf");
$arrlen=count($col);
for($i=0;$i<$arrlen;$i++){
echo $col[$i]."
";
}
Associative Array Example
Example 1
$col['Green']="#123";
$col['Red']="#123";
$col['White']="#fff";
echo $col['Red'];
Example 2
$col=array("Green"=>"#123","White"=>"#231","Red"=>"#44");
echo $col['Green'];
Example 3
$col=array("Green"=>"#123","Green"=>"#231","Red"=>"#44");
echo $col['Green'];
Example 4
$col['Green']="#123";
$col['Red']="#123";
$col['White']="#fff";
foreach($col as $val=>$n){
echo $val."
";
}
Multi Multidimensional Array Example
Example 1
$arr=array(
array(1,2,3),
array(4,5,6),
array(7,8,9)
);
echo $arr[1][2];
}
Example 2
$arr=array(
array(1,2,3),
array(4,5,6),
array(7,8,9)
);
for($r=0;$r<3;$r++){
for($c=0;$c<3;$c++){
echo $arr[$r][$c];
}
echo "
";
}
}
agyanadda
Author & Editor
Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.
0 $type={blogger}:
Post a Comment