PHP Array1
PHP Array1
S. S. Dohe
Lect, Computer Engineering
Government Polytechnic Yavatmal
COMPETENCY
Develop simple web-based application
using PHP language
Unit-II
Arrays, Functions and Graphics
CO
b) Perform operations based on arrays and
graphics.
UO
2 a) Manipulate the given type of arrays to get
the desired result.
Introduction
Introduction
Adding and Deleting Elements
Adding and Deleting Elements
unset () in Arrays:
isset() in Arrays:
Indexed Array
Indexed Array
Indexed Array
Associated Array
Associated Array
Associated Array
Multidimensional Arrays
Multidimensional Arrays
extract() Function
• That is it converts array keys into variable names and array values into
variable value.
• Syntax:
int extract($input_array, $extract_rule, $prefix)
• $prefix: This parameter is optional. This parameter specifies the prefix. The
prefix is automatically separated from the array key by an underscore
character
Example :
Example :
implode() Function
• The implode() is a built-in function in PHP and is used to join the
elements of an array. implode() is an alias for PHP | join()
function and works exactly same as that of join() function.
• We basically join array elements with a string. Just like join() function ,
implode() function also returns a string formed from the elements of
an array.
• Syntax :
string implode(separator,array)
Parameters:
• The explode function accepts three parameters of which two are compulsory and one is optional.
• The function returns an array in flip order, i.e. keys from array become
values and values from array become keys.
• The values of the array need to be valid keys, i.e. they need to be
either integer or string.
array_flip() function
• Syntax:
array_flip(array)
• Parameter Values
• There are several ways to traverse arrays in PHP, and the one you
choose will depend on your data and the task you’re performing.
o Using a for Loop
o The Iterator Functions
o The foreach Construct
Using a for Loop
• for loop: Entry control loop:
PHP engine executes statement(s) inside the for loop, until the given conditional
expression evaluates to false.
Syntax:
for(initialization; conditional expression; increment/decrement)
{
statement(s);
}
OR
OR
Note: When you know exactly how many # of times the loop is going
to get execute, use for loop.
PHP Array Iterator Functions
• PHP Array Iterative functions can be used to access the elements as :
• first element,
• last element,
• previous element or
• next element
• The concept of pointers is used here.
• When an array is created a hidden array pointer stores the address of first
element by default.
• When any of iterative functions is called that action is performed on array and an
element is returned.
• PHP array iterative functions treat array as a doubly linked list of similar values.
Each element has direct access to the next and previous element.
Types of PHP Array Iterator Functions
1. current()
• Current() function returns the value of the element whose address is currently
stored in the array pointer.
• Syntax:
• $varName=current($arrayName);
• $varName is the name of the variable that accepts the output of the current
function.
• $arrayName is the name of the array in which you want access the current
element.
• The array pointer is not updated after the current function is called.
Types of PHP Array Iterator Functions
2. next ()
• next() function first updates the array pointer with the address of the next
element. It is relative to the address of the current element. It then returns the
value of this element.
• Syntax:
• $varName=next($arrayName);
• $varName is the name of the variable that accepts the output of the next
function.
• $arrayName is the name of the array from which you want to get the next
element relative to the current pointer.
• The array pointer is updated after the next function is called.
• If the array pointer reaches the end of array and the next function is called array
pointer stays at the last element.
Types of PHP Array Iterator Functions
3. prev ()
• prev() function updates the array pointer with the address of previous element. It is
relative to address of the current element. It then returns the value of this newly located
element.
• Syntax:
• $varName=prev($arrayName);
• $varName is the name of the variable that accepts the output of the prev function.
• $arrayName is the name of the array from which you want to read the previous element
relative to the current pointer.
• Syntax:
• $varName=reset($arrayName);
• $varName is the name of the variable that accepts the output of reset function.
• $arrayName is the name of the array in which you want to reach to the first
element.
• Syntax:
• $varName=last($arrayName);
• $varName is the name of the variable that accepts the output of end function.
• $arrayName is the array in which you want to move the array pointer to last
element.
• each(): Returns the key and value of the current element as an array
and moves the iterator to the next element in the array
foreach($arrayName as $variableName)
{
statement(s);
}
OR
foreach($arrayName as $variableName):
statement(s);
endforeach;
The foreach Construct
• Syntax2:
<!DOCTYPE html>
<html>
<body>
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) {
echo "$x <br>";
} Output:
?> red
</body> green
</html> blue
yellow
The foreach Construct
• Example Code:
<!DOCTYPE html>
<html>
<body>
<?php
$fruits = array(
"Apple" => "red",
"banana" => "yellow",
"orange" => "orange"
);
foreach ($fruits as $key => $value) {
echo "The color of the ". $key . " is " . $value . "<br>"; }
?>
</body>
</html>
Functions
• PHP function is a piece of code that can be reused many times.
• It can take input as argument list and return value.
• There are thousands of built-in functions in PHP.
• Code Reusability: PHP functions are defined only once and can be
invoked many times, like in other programming languages.
• Less Code: It saves a lot of code because you don't need to write the
logic many times. By the use of function, you can write the logic only
once and reuse it.
• Functions save on compile time—no matter how many times you call
them, functions are compiled only once for the page.
• Functions in a PHP program can be built-in or user-defined
Functions
• To define a function, use the following syntax:
1. user-defined Functions
2. Variable Functions
3. Anonymous Functions