Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

Array --->

A type of datastructure. It allows us to store multiple elements under


a single variable.

The arrays are helpful in creating a list of element of similar types.

The elements can be accessed based on the index number

below example shows how to create an array.

<?php
$new=array(1,2,3,4,5);
echo $new[2];
?>

Types of arrays in PHP --->

there are three types of arrays : --->

1. Indexed or Numeric Arrays --->


2. Associative Arrays -->
3. Multidimensional Arrays -->

1. An array where values are stored linearly with a numeric index.


These type of arrays can be used to store any type of elements, but the condition
is an index
should always be a number.

By default the index will start from 0.


and use array function to create a new array.

how to create and use indexed array

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

2nd way to create and use indexed arrays


<?php
$name[0]="One Piece";
$name[1]="Bleach";
$name[2]="DBZ";
$name[3]="Goblin";
$name[4]="Death Note";
$name[5]="Pokemon";
echo $name[0]."<br>";
echo $name[1]."<br>";
echo $name[2]."<br>";
echo $name[3]."<br>";
echo $name[4]."<br>";
echo $name[5]."<br>";
?>

3rd way to create and use indexed arrays


<?php
$name[0]="One Piece";
$name[1]="Bleach";
$name[2]="DBZ";
$name[3]="Goblin";
$name[4]="Death Note";
$name[5]="Pokemon";
foreach ($name as $value)
{
echo $value."<br>";
}
?>

Associative Arrays --->

These are similar to indexed arrays but instead of index being a number every value
can be user
defined key of string type

example of creating and using associative arrays.


<?php
$name=array("One"=>1,"Two"=>2,"Three"=>3);
echo $name["One"];
echo $name["Two"];
?>

Multidimensional Arrays -->

So, arrays which store another array at each index instead of single element are
called
multi dimensional arrays.

example of multidimensional arrays -->

<?php
$name=array((array("Name"=>"Aman","mob"=>1234567890)),
array("Name"=>"Vishal","mob"=>345678901));
echo $name[0]["mob"];
?>

Functions in array -->

array function ---> used to create an array.

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

count function in PHP --->

it will count all the elements in an array.

<?php
$name=array(1,2,3,4,5);
echo count($name);
?>

sort function in PHP -->

this function will sort the values

<?php
$name=array(4,5,10,1,3);
sort($name);
foreach($name as $n)
{
echo $n;
}
?>

array_reverse function -->

so this function as the name suggests will return elements in reversed


order

<?php
$name=array(4,5,10,1,3);
$r_name=array_reverse($name);
foreach($r_name as $n)
{
echo $n." ";
}
?>

array_search function -->

searches for the specified value in an array. it returns the key


if search is successful.

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

push function --> adds the element to the last.

<?php
$names=array("Newton","Einstein");
array_push($names,"Curie");
print_r($names);
?>

print_r --> this will print the array in a readable format.

array_shift ===> will remove the first element of the array.

<?php
$names=array("Newton","Einstein");
array_shift($names);
print_r($names);
?>

this will remove Newton.

array_pop --> will remove the last element of array.

Traversal functions of Arrays -->

key --> this will give the current key.


this will just print current key.

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).

prev ---> this will go to the prev value.

reset --> this function as the name suggests will reset the value to the beginning.

example of next and reset function -->


<?php
$name=array("sunday"=>1,"monday"=>2,"tuesday"=>3);
next($name);
print_r(next($name));
reset($name);
print_r(current($name));
?>

use of constants in PHP


<?php
define("GREETING", "Welcome to W3Schools.com!");
echo GREETING;
?>

You might also like