Unit 2-1
Unit 2-1
Graphics
Arrays
🞂 Introduction to arrays:
Array is a collection of similar type of
elements, but in PHP elements of mixed
type together in single array
Each element has two parts key and value
The key represents the index at which the
value of the element can be stored.
The keys are positive integers that are in
ascending order.
Creating & Manipulating arrays
For creating array in PHP we use array()
construct.
Syntax:
$array_name = array(value)
Indexed Array
These arrays can store numbers, strings and any object but
their index will be represented by numbers. By default
array index starts from zero.
Example
Following is the example showing how to create and access
numeric arrays.
Here we have used array() function to create array. This
function is explained in function reference.
Indexed Array Example:
Associated array
🞂 There arrays are with named keys
🞂 These array are with name and value pair
Example:
<? php
$ age = array(
“Riya” =>25;
“Diya” =>22;
“Geeta” =>28;
);
echo”<pre>”;
print_r($age);
echo”<pre>”;
<? php
$course [“CO”]= “Computer Engg.”;
$course [“IF”]= “Information Tech.”;
$course [“EJ”]= “Electronics and Telecomm.”;
extract($course);
echo “CO=$CO <br>”;
echo “IF=$IF <br>”;
echo “EJ=$EJ <br>”;
?>
O/P: CO=Computer Engg.
IF=Information Tech.
EJ=Electronics and Telecomm.
list() function is also used to extract variables from an array.
<? php
$course [“0”]= “Computer Engg.”;
$course [“1”]= “Information Tech.”;
$course [“2”]= “Electronics and Telecomm.”;
list($one,$two)=$course;
echo $one, “ <br>”;
echo $two;
?>
o/p: Computer Engg.
Information Tech.
Compact() function
🞂 This function is opposite to extract() function.
🞂 It returns an array with all the variables added to it.
🞂 Each parameter can be either a string containing the
name of the variable or an array of variable names.
🞂 The compact() functions create associative array
whose key value are the variable name and whose
values are the variable values.
Example:
<? php
$var1= “PHP”;
$var2= “JAVA”;
$var3= compact(“var1”,”var2”);
print_r($var3);
?>
o/p: array ([var1]=>PHP [var2]=>JAVA)
Implode() Function:
🞂 The implode() is a built-in function in PHP and is used to
join the elements of an array.
🞂 The implode() function returns a string from the elements
of an array.
🞂 If we have an array of elements, we can use the implode()
function to join them all to from one string.
Syntax:
string implode(separator,array);
Example:
In which an array is “imploded” into a text string and display
<? php
$course [“0”]= “Computer Engg.”;
$course [“1”]= “Information Tech.”;
$course [“2”]= “Electronics and Telecomm.”;
$text=implode(“,”.$course);
echo $text;
?>
O/p:
Computer Engg.,Information Tech.,Electronics and
Telecomm.
Explode() Function:
The explode() function breaks a string into an array.
The explode() ia a built in function in PHP used to split a
string in different strings.
Syntax:
array explode (separator,OriginalString,NoOfElement);
Example:
<? php
$str = “PHP:Welcome to the world of PHP”;
print_r (explode(“ ”,$str));
?>
o/p:
array([0]=>PHP: [1]=>Welcome [2]=>to [3]=>the
[4]=>world [5]=>of [6]=>PHP)
Array_flip() function:
The array_flip() function flips/exchanges all keys with
their associated values in an array.
Syntax:
array_flip(array);
Example:
<? php
$a1=array (“CO”=> “Computer Engg”, “IF”=>
“Information Tech”, “EJ”=> “Electronics and
Telecomm”);
$result = array_flip($a1);
print_r($result);
?>
O/P: array([Computer Engg]=>CO [Information
Tech]=>IF [Electronics and Telecomm]=>EJ)