[go: up one dir, main page]

0% found this document useful (0 votes)
43 views29 pages

Internet Applications: PHP Arrays, HTML Forms

This document discusses PHP arrays and functions related to arrays. It covers numerically indexed arrays, associative arrays, looping through arrays, arrays of arrays, and functions like count(), sort(), ksort(), asort() among others. It provides examples of creating, accessing, and manipulating PHP arrays.

Uploaded by

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

Internet Applications: PHP Arrays, HTML Forms

This document discusses PHP arrays and functions related to arrays. It covers numerically indexed arrays, associative arrays, looping through arrays, arrays of arrays, and functions like count(), sort(), ksort(), asort() among others. It provides examples of creating, accessing, and manipulating PHP arrays.

Uploaded by

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

Internet Applications

PHP Arrays, HTML Forms

Instructor: Ashok Singh Sairam


ashok@iitg.ac.in
PHP Arrays: Numerically indexed arrays (1)
• The keyword array is a constructor
• Integer indices starting from 0
PHP Arrays: Numerically indexed arrays (2)
• The keyword array is a constructor <?php
• Integer indices starting from 0 $words=array();
$words[]="Hello";
• Creating arrays
$words[]="World";
• Start from an empty array and add items
echo $words[1];
?>
Output: World
PHP Arrays: Numerically indexed arrays (3)
• The keyword array is a constructor
• Integer indices starting from 0
<?php
• Creating arrays
• Start from an empty array and add items $words=array(“hello”,”world”);
• Copy one array to the other using the “=“ $greet=$words;
?>
PHP Arrays: Numerically indexed arrays (4)
• The keyword array is a constructor <?php
$numbers = range(1,10);
• Integer indices starting from 0 ?>
• Creating arrays $numbers[0] is assigned 1,
• Start from an empty array and add items $numbers[1] is assigned 2,
• Copy one array to the other using the “=“ and so on
• Use the range() function to auto populate an array
PHP Arrays: Associative arrays
• In associative arrays, we associate a key (or index) with each value
key=>value
PHP Arrays: Associative arrays
• In associative arrays, we associate a key (or index) with each value
key=>value

<?php
$MA518=array("instructor"=>"Ashok","course name"=>"DBMS");
echo $MA518['course name'] ."\t". $MA518['instructor'], "\n";
?>
Output
DBMS Ashok
Dumping Arrays
• print_r(): shows information about a variable, good for debugging
Dumping Arrays
• print_r(): shows information about a variable, good for debugging
• Array values will presented as key and value.
Dumping Arrays
• print_r(): shows information about a variable, good for debugging
• Array values will presented as key and value.

<?php Output
$MA518=array("instructor"=>"Ashok","course Array
name"=>"DBMS"); (
echo("<pre>\n"); [instructor] => Ashok
print_r($MA518); [course name] => DBMS
echo("</pre>"); )
?>
var_dump vs. print_r
• var_dump displays structured information about
variables/expressions including its type.
var_dump vs. print_r
• var_dump displays structured information about
variables/expressions including its type.

<?php Output
array(2) {
$MA518=array("instructor"=>"Ashok","course ["instructor"]=> string(5) "Ashok"
name"=>"DBMS"); ["course name"]=> string(4) "DBMS"
echo("<pre>\n"); }
var_dump($MA518);
echo("</pre>");
?>
Looping through an array
• Numerically indexed arrays: As the array is indexed by numbers we
can use a for loop
• Loop foreach: Iterate through each item of an array
• specially designed for arrays

MA 518: Database Management Systems 13


Looping through an array
• Numerically indexed arrays: As the array is indexed by numbers we
can use a for loop
• Loop foreach: Iterate through each item of an array
• specially designed for arrays
<?php <?php
$words=array("Hello","World"); $words=array("Hello","World");
for($i=0,$i<2,$i++) foreach ($words as $singleword)
echo $words[$i],"\n" ; echo $singleword,"\n" ;
?> ?>

MA 518: Database Management Systems 14


Looping through an Array (2)
• foreach loop in Associative arrays
• can iterate through each item as in case of numerically indexed arrays
• we can incorporate the keys as well, $key=>$value
Looping through an Array (2)
• foreach loop in Associative arrays
• can iterate through each item as in case of numerically indexed arrays
• we can incorporate the keys as well, $key=>$value

<?php
header('Content-type: text/plain');
$MA518=array("instructor"=>"Ashok","cname"=>"DBMS"); Output
foreach($MA518 as $k => $val) { Key: instructor Value: Ashok
echo "Key: " , $k , " Value: " , $val , "\n"; Key: cname Value: DBMS
}
?>
Counted Loop
• count() – returns the number of items in the array
Counted Loop
• count() – returns the number of items in the array

<?php
header('Content-type: text/plain');
$MA518=array("Hello","World");
for($i=0; $i<count($MA518); $i++) {
echo "Index: " , $i , " Value: " , $MA518[$i] ,
"\n";
}
?>
Output
Index: 0 Value: Hello
Index: 1 Value: World
Arrays of Arrays
• Array student has two items, student and courses, which are arrays

<?php
header('Content-type: text/plain');
$student=array(
"address"=>array(
"name"=>"Tom","Roll"=>123), Output:
“courses"=>array( Roll no: 123
"cno"=>"MA518", Course Name: DBMS
"cname"=>"DBMS")
);
echo "Roll no: ", $student['address']['Roll'], "\n",
"Course Name: ", $student[‘courses']['cname'],"\n";
?>
Arrays of Arrays
• Array student has two items, student and courses, which are arrays

<?php
header('Content-type: text/plain');

$student=array(
Output:
"address"=>array( Roll no: 123
"name"=>"Tom","Roll"=>123), Course Name: DBMS
“courses"=>array(
"cno"=>"MA518",
"cname"=>"DBMS")
);
echo "Roll no: ", $student['address']['Roll'], "\n",
"Course Name: ", $student[‘courses']['cname'],"\n";
?>
Array Functions
• array_key_exists($key,$ar): Returns TRUE if key is set in array ar
Array Functions
• array_key_exists($key,$ar): Returns TRUE if key is set in array ar
• isset($ar[‘key’]): Returns TRUE if key is set in the array
Array Functions
• array_key_exists($key,$ar): Returns TRUE if key is set in array ar
• isset($ar[‘key’]): Returns TRUE if key is set in the array
• count($ar): Count the number of elements in the array
Array Functions
• array_key_exists($key,$ar): Returns TRUE if key is set in array ar
• isset($ar[‘key’]): Returns TRUE if key is set in the array
• count($ar): Count the number of elements in the array
• is_array($ar): Returns TRUE if variable ar is an array
Array Functions
• array_key_exists($key,$ar): Returns TRUE if key is set in array ar
• isset($ar[‘key’]): Returns TRUE if key is set in the array
• count($ar): Count the number of elements in the array
• is_array($ar): Returns TRUE if variable ar is an array
• sort($ar): Sorts the array values (losses keys)
<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
?>
Output: 2, 4, 6, 11, 22
Array Functions
• array_key_exists($key,$ar): Returns TRUE if key is set in array ar
• isset($ar[‘key’]): Returns TRUE if key is set in the array
• count($ar): Count the number of elements in the array
• is_array($ar): Returns TRUE if variable ar is an array
• sort($ar): Sorts the array values (losses keys)
• ksort($ar): Sorts the array by keys
<?php
$age = array("Peter"=>"35", "Ben"=>"37",
"Joe"=>"43");
ksort($age);
?>
Output: Ben, Joe, Peter
Array Functions
• array_key_exists($key,$ar): Returns TRUE if key is set in array ar
• isset($ar[‘key’]): Returns TRUE if key is set in the array
• count($ar): Count the number of elements in the array
• is_array($ar): Returns TRUE if variable ar is an array
• sort($ar): Sorts the array values (losses keys)
• ksort($ar): Sorts the array by keys
• asort($ar): Sorts the array by values, keep association
<?php
$age = array("Peter"=>"35", "Ben"=>"37",
"Joe"=>"43");
asort($age);
?>
Output: 35, 37, 43
Array Functions
• array_key_exists($key,$ar): Returns TRUE if key is set in array ar
• isset($ar[‘key’]): Returns TRUE if key is set in the array
• count($ar): Count the number of elements in the array
• is_array($ar): Returns TRUE if variable ar is an array
• sort($ar): Sorts the array values (losses keys)
• ksort($ar): Sorts the array by keys
• asort($ar): Sorts the array by values, keep association
• shuffle($ar): Shuffles the array into random order
Thank You

MA 518: Database Management Systems 29

You might also like