0% found this document useful (0 votes)
25 views

Web Based Application Development With PHP: WBP (22619) Lect 8

The document discusses PHP arrays and functions for manipulating arrays. It covers multidimensional arrays, traversing arrays using foreach, iterator functions, and for loops. Functions for extracting data from arrays like extract(), implode(), explode(), and array_flip() are explained.

Uploaded by

Govind Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Web Based Application Development With PHP: WBP (22619) Lect 8

The document discusses PHP arrays and functions for manipulating arrays. It covers multidimensional arrays, traversing arrays using foreach, iterator functions, and for loops. Functions for extracting data from arrays like extract(), implode(), explode(), and array_flip() are explained.

Uploaded by

Govind Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Web Based Application

development with PHP


WBP (22619)
Lect 8

G. K. YADAV
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
2 b) Apply implode, explode functions on the given array
Multidimensional Array
$favorites = array(
"Dave Punk" => array(
"mob" => "5689741523",
"email" => "[email protected]",
),
“Smith" => array(
"mob" => "2584369721",
"email" => "[email protected]",
),
"John Flinch" => array(
"mob" => "9875147536",
"email" => "[email protected]",
)
);
Traversing Arrays
• The most common task with arrays is to do something
with every element—for instance, sending mail to each
element of an array of addresses, updating each file in
an array of filenames, or adding up each element of an
array of prices.
• 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 The foreach Construct
o The Iterator Functions
o Using a for Loop
The foreach Construct
e.g. $addresses = array("[email protected]",
"[email protected]");
foreach ($addresses as $value) { }
An alternative form of foreach gives you access to the
current key:
e.g. $person = array('name' => "Fred", 'age' => 35, 'wife' =>
"Wilma");
foreach ($person as $key => $value) {}
In this case, the key for each element is placed in $key and
the corresponding value is placed in $value.
The foreach construct does not operate on the array itself,
but rather on a copy of it.
The Iterator Functions
Every PHP array keeps track of the current element you’re working with;
the pointer to the current element is known as the iterator.
PHP has functions to set, move, and reset this iterator.
current(): Returns the element currently pointed at by the iterator
reset():Moves the iterator to the first element in the array and returns it
next(): Moves the iterator to the next element in the array and returns it
prev(): Moves the iterator to the previous element in the array and
returns it
end(): Moves the iterator to the last element in the array and returns it
each(): Returns the key and value of the current element as an array and
moves the iterator to the next element in the array
key(): Returns the key of the current element
The Iterator Functions
To copy all of an array’s values into variables, use
the list() construct.
while (list($key, $value) = each($addresses))
{
echo "{$key} is {$value}<br />\n";
}
This approach does not make a copy of the array,
as foreach does. This is useful for very large arrays
when you want to conserve memory.
Using a for Loop
If you know that you are dealing with an indexed array, where
the keys are consecutive integers beginning at 0, you can use a
for loop to count through the indices.
The for loop operates on the array itself, not on a copy of the
array, and processes elements in key order regardless of their
internal order.
$addresses = array("[email protected]",
"[email protected]");
$addressCount = count($addresses);
for ($i = 0; $i < $addressCount; $i++) {
$value = $addresses[$i];
echo "{$value}\n";
}
Extracting data from arrays
The extract() function is an inbuilt function in PHP. The extract() function
does array to variable conversion.
That is it converts array keys into variable names and array values into
variable value
Syntax:
int extract($input_array, $extract_rule, $prefix)
$input_array: This parameter is required. This specifies the array to use.
$extract_rule: This parameter is optional. The extract() function checks
for invalid variable names and collisions with existing variable names.
This parameter specifies how invalid and colliding names will be treated.
e.g. EXTR_OVERWRITE, EXTR_SKIP, EXTR_PREFIX_SAME, EXTR_PREFIX_ALL
$prefix: This parameter is optional. This parameter specifies the prefix.
The prefix is automatically separated from the array key by an
underscore character
Extracting data from arrays
<?php

// input array
$state = array("AS"=>"ASSAM", "OR"=>"ORRISA",
"KR"=>"KERELA");

extract($state);

// after using extract() function


echo"\$AS is $AS\n\$KR is $KR\n\$OR is $OR";

?>
Implode
Imploding and Exploding are couple of important
functions of PHP that can be applied on strings or
arrays.
PHP provides us with two important built-in
functions implode() and explode() to perform
these operations.
string implode(string separator, array strings)
Returns a string created by joining every element
in strings with separator.
Implode
<?php

$array1 = array('www', 'geeksforgeeks', 'org');


echo(implode('.',$array1)."<br>");

$array2 = array('H', 'E', 'L', 'L', 'O');


echo(implode($array2));
?>
Explode
Syntax:
array explode (delimiter, string, limit)

Returns an array of substrings created by splitting


string wherever separator is found
If supplied, a maximum of limit substrings will be
returned, with the last substring returned
containing the remainder of the string
If separator is not found, returns the original string
Explode
Syntax:
array explode (delimiter, string, limit)

Returns an array of substrings created by splitting string wherever separator is found


If supplied, a maximum of limit substrings will be returned, with the last substring returned
containing the remainder of the string
If separator is not found, returns the original string
<?php
// PHP code to illustrate the working of explode()
$str1 = '1,2,3,4,5';
$arr = explode(',',$str1);
foreach($arr as $i)
echo($i.'<br>');
?>
Array flip
This built-in function of PHP is used to exchange elements within an
array, i.e., exchange all keys with their associated values in an array and
vice-versa.
Syntax:
array array_flip($array)
Parameters: The function takes only one parameter $array that refers to
the input array.

Return Type: This function returns another array, with the elements
exchanged or flipped, it returns null if the input array is invalid.

If multiple values in the array are the same, then on the use of the
array_flip() function, only the key with the maximum index (after swap)
will be added to the array.
Array flip
<html>
<body>

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$result=array_flip($a1);
print_r($result);
?>

</body>
</html>

You might also like