Professional Documents
Culture Documents
PHP Day1
PHP Day1
<?php
$new=array(1,2,3,4,5);
echo $new[2];
?>
<?php
$name=array("John","Rock","Eddy","Ram","Shyam");
echo "Accessing the elements directly";
echo $name[0];
echo $name[1];
echo $name[2];
echo $name[3];
echo $name[4];
echo $name[5];
?>
These are similar to indexed arrays but instead of index being a number every value
can be user
defined key of string type
So, arrays which store another array at each index instead of single element are
called
multi dimensional arrays.
<?php
$name=array((array("Name"=>"Aman","mob"=>1234567890)),
array("Name"=>"Vishal","mob"=>345678901));
echo $name[0]["mob"];
?>
array_change_key_case --> this function changes the case of all keys in an array.
<?php
$name=array("sunday"=>1,"monday"=>2,"tuesday"=>3);
print_r(array_change_key_case($name,CASE_UPPER));
?>
array_chunk function --> split array into chunks or many parts.
<?php
$name=array("sunday"=>1,"monday"=>2,"tuesday"=>3);
print_r(array_chunk($name,2));
?>
<?php
$name=array(1,2,3,4,5);
echo count($name);
?>
<?php
$name=array(4,5,10,1,3);
sort($name);
foreach($name as $n)
{
echo $n;
}
?>
<?php
$name=array(4,5,10,1,3);
$r_name=array_reverse($name);
foreach($r_name as $n)
{
echo $n." ";
}
?>
<?php
$name=array(4,5,10,1,3);
$key=array_search(10,$name);
print($key);
?>
unshift function --> adds the element to the beginning of the array.
<?php
$names=array("Newton","Einstein");
array_unshift($names,"Curie");
print_r($names);
?>
<?php
$names=array("Newton","Einstein");
array_push($names,"Curie");
print_r($names);
?>
<?php
$names=array("Newton","Einstein");
array_shift($names);
print_r($names);
?>
current --> this will give the value of the current key.
the value of the current key will be printed.
next ---> this will move the array pointer to the next value(not the key).
reset --> this function as the name suggests will reset the value to the beginning.