100% found this document useful (1 vote)
808 views

Practical No. 4: Write A PHP Program For Creating and Manipulating - A) Indexed Array, B) Associative Array, C) Multidimensional Array

The document discusses PHP arrays, including indexed, associative, and multidimensional arrays. It provides examples of creating each array type and outputting values. The practical exercise portion demonstrates PHP programs to create and manipulate each array type, including outputting values with and without loops. It concludes that the given experiment on creating and manipulating the three array types was successfully completed.

Uploaded by

huzaifa Dabir
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
100% found this document useful (1 vote)
808 views

Practical No. 4: Write A PHP Program For Creating and Manipulating - A) Indexed Array, B) Associative Array, C) Multidimensional Array

The document discusses PHP arrays, including indexed, associative, and multidimensional arrays. It provides examples of creating each array type and outputting values. The practical exercise portion demonstrates PHP programs to create and manipulate each array type, including outputting values with and without loops. It concludes that the given experiment on creating and manipulating the three array types was successfully completed.

Uploaded by

huzaifa Dabir
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/ 4

Web Based Application Development Using PHP(22619)

Practical No. 4: Write a PHP Program for creating and manipulating –


a) Indexed array, b) Associative array,
c) Multidimensional array

I. Practical Significance
An array is a special variable, which can hold more than one value at a time. For example if
you want to store 100 numbers then instead of defining 100 variables its easy to define an array
of 100 length.

II. Minimum Theoretical Background


In PHP, the array() function is used to create an array:
In PHP, there are three types of arrays:
• Indexed arrays - Arrays with a numeric index
• Associative arrays - Arrays with named keys
• Multidimensional arrays - Arrays containing one or more arrays

Indexed Arrays
There are two ways to create indexed arrays:
The index can be assigned automatically (index always starts at 0), like this:
$cars = array("Volvo", "BMW", "Toyota");
or the index can be assigned manually:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";

Associative Arrays
Associative arrays are arrays that use named keys that you assign to them.
There are two ways to create an associative array:
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
or:
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";

Multidimensional Arrays
A multidimensional array is an array containing one or more arrays.
PHP supports multidimensional arrays that are two, three, four, five, or more levels deep.
However, arrays more than three levels deep are hard to manage for most people.

• Two-dimensional Arrays
A two-dimensional array is an array of arrays (a three-dimensional array is an array of arrays
of arrays). We can store the data from a table in a two-dimensional array, like this:
$cars = array
(
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);

1
Web Based Application Development Using PHP(22619)

Get The Length of an Array - The count() Function


The count() function is used to return the length (the number of elements) of an array:
Example:
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>

III. Exercise:
1) Indexed Array
Input:
<?php
//Indexed Array
echo "<h2>INDEXED ARRAY</h2>";
$cars = array("Volvo", "BMW", "Toyota");
echo "<b>Printing the array without using for loop</b><br>";
echo "I like " . $cars[0] .", ". $cars[1] ." and ". $cars[2] .".";

echo "<br><br><b>Printing the array using for loop</b><br>";


$arrlength = count($cars);
for($i = 0; $i < $arrlength; $i++) {
echo $cars[$i];
echo "<br>";
}
?>

Output:
INDEXED ARRAY

Printing the array without using for loop


I like Volvo, BMW and Toyota.

Printing the array using for loop


Volvo
BMW
Toyota

2) Associative Array
Input:
<?php
//Associative Array
echo "<br><h2>ASSOCIATIVE ARRAY</h2>";
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "<b>Printing the array without using loop</b><br>";
echo "Peter is " . $age['Peter'] . " years old.";

echo "<br><br><b>Printing the array using foreach loop</b><br>";


foreach($age as $key => $value) {
echo "Name: " . $key . ", Age: " . $value;
echo "<br>";
}
?>

2
Web Based Application Development Using PHP(22619)

Output:
ASSOCIATIVE ARRAY

Printing the array without using loop


Peter is 35 years old.

Printing the array using foreach loop


Name: Peter, Age: 35
Name: Ben, Age: 37
Name: Joe, Age: 43

3) Multidimensional Array
Input:
<?php
//Multidimensional Array
echo "<br><h2>MULTIDIMENSIONAL ARRAY</h2>";
$cars = array
(
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);

echo "<b>Printing the array without using loop</b><br>";


echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";
echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";

echo "<br><br><b>Printing the array using for and foreach loop</b><br>";


$favorites = array(
"Dave Punk" => array(
"mob" => "5689741523",
"email" => "[email protected]",
),
"Dave Punk" => array(
"mob" => "2584369721",
"email" => "[email protected]",
),
"John Flinch" => array(
"mob" => "9875147536",
"email" => "[email protected]",
)
);

// Using for and foreach in nested form


$keys = array_keys($favorites);
for($i = 0; $i < count($favorites); $i++)
{
echo $keys[$i] . "<br>";
foreach($favorites[$keys[$i]] as $key => $value)
{
echo $key . " : " . $value . "<br>";
}
echo "<br>";
}
?>

3
Web Based Application Development Using PHP(22619)

Output:
MULTIDIMENSIONAL ARRAY

Printing the array without using loop


Volvo: In stock: 22, sold: 18.
BMW: In stock: 15, sold: 13.
Saab: In stock: 5, sold: 2.
Land Rover: In stock: 17, sold: 15.

Printing the array using for and foreach loop


Dave Punk
mob : 2584369721
email : [email protected]

John Flinch
mob : 9875147536
email : [email protected]

IV. Conclusion
Thus, we have successfully completed the given experiment based on:
Program for creating and manipulating- a) Indexed array, b) Associative array, c) Multidimensional
array

You might also like