PHP 3
PHP 3
What is an Array?
An array is a special variable that can hold many values under a single name, and you can access the
values by referring to an index number or name.
The keys of the indexed array are integers that start at 0. Typically, you use indexed arrays when you
want to access the elements by their positions.
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
****OUTPUT****
I like Volvo, BMW and Toyota.
• 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);
?>
Exercise:
www.ourcreativeinfo.in
PHP – 3
Example
<html>
<body>
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
</body>
</html>
****OUTPUT****
I like Volvo, BMW and Toyota.
www.ourcreativeinfo.in
PHP – 3
Example
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
****OUTPUT****
Peter is 35 years old.
www.ourcreativeinfo.in
PHP – 3
yellow
Volvo 22 18
BMW 15 13
Saab 5 2
Land Rover 17 15
www.ourcreativeinfo.in
PHP – 3
We can store the data from the table above 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)
);
Now the two-dimensional $cars array contains four arrays, and it has two indices:
row and column.
To get access to the elements of the $cars array we must point to the two indices
(row and column):
Example
<html>
<body>
<?php
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
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>";
?>
</body>
www.ourcreativeinfo.in
PHP – 3
</html>
****OUTPUT****
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.
}
?>
</body>
</html>
****OUTPUT****
Row number 0
• Volvo
• 22
• 18
Row number 1
• BMW
• 15
• 13
Row number 2
• Saab
• 5
• 2
Row number 3
• Land Rover
• 17
• 15
www.ourcreativeinfo.in
PHP – 3
✓ Example:
<html>
<body>
<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
$clength = count($cars);
for($x = 0; $x < $clength; $x++)
{
echo $cars[$x];
echo "<br>";
}
?>
</body>
</html>
****OUTPUT****
BMW
Toyota
Volvo
✓ The following example sorts the elements of the $numbers array in ascending
numerical order:
Example:
<html>
<body>
<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
www.ourcreativeinfo.in
PHP – 3
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++)
{
echo $numbers[$x];
echo "<br>";
}
?>
</body>
</html>
****OUTPUT****
2
4
6
11
22
echo $cars[$x];
echo "<br>";
}
?>
</body>
</html>
****OUTPUT****
Volvo
Toyota
BMW
✓ The following example sorts the elements of the $numbers array in
descending numerical order:
Example:
<html>
<body>
<?php
$numbers = array(4, 6, 2, 22, 11);
rsort($numbers);
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++)
{
echo $numbers[$x];
echo "<br>";
www.ourcreativeinfo.in
PHP – 3
}
?>
</body>
</html>
****OUTPUT****
22
11
6
4
2
Example:
<html>
<body>
<?php
$age = array("Peter"=>"39", "Ben"=>"37", "Joe"=>"43");
asort($age);
foreach($age as $x => $x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
www.ourcreativeinfo.in
PHP – 3
echo "<br>";
}
?>
</body>
</html>
****OUTPUT****
Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43
Sort Array (Ascending Order), According to Key - ksort()
✓ The following example sorts an associative array in ascending order,
according to the key:
Example:
<html>
<body>
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age);
foreach($age as $x => $x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
</body>
</html>
****OUTPUT****
Key=Ben, Value=37
Key=Joe, Value=43
www.ourcreativeinfo.in
PHP – 3
Key=Peter, Value=35
Example:
<html>
<body>
<h2>Project Pizza Topping Selector</h2>
<form method="post" action="pizza.php">
Select your favourite pizza toppings: <br />
<input type="checkbox" name="toppings[]" value="tomato">Tomato</input>
<input type="checkbox" name="toppings[]" value="onion">Onion</input>
<input type="checkbox" name="toppings[]" value="jalapenos">Jalapeno
peppers</input>
<input type="checkbox" name="toppings[]" value="olives">Olives</input>
<input type="checkbox" name="toppings[]" value="mint">Mint</input>
<input type="checkbox" name="toppings[]" value="pineapple">Pineapple</input>
<input type="checkbox" name="toppings[]" value="bacon">Bacon</input>
<input type="checkbox" name="toppings[]" value="chicken">Chicken</input>
<input type="checkbox" name="toppings[]" value="ham">Ham</input>
<input type="checkbox" name="toppings[]" value="anchovies">Anchovies</input>
www.ourcreativeinfo.in
PHP – 3
• Illustrates what the form looks like. And here’s the code for the form
submission script (pizza.php):
<html >
<body>
<h2>Pizza Topping Selector</h2>
You selected the following toppings: <br />
<ul>
<?php
foreach ($_POST['toppings'] as $t) {
echo "<li>$t</li> \r\n";
}
?>
</ul>
www.ourcreativeinfo.in
PHP – 3
</body>
</html>
www.ourcreativeinfo.in
PHP – 3
Create an array by using the elements from one "keys" array and one "values"
array:
Example :
<html>
<body>
<?php
$name= array("John","Peter","Harry");
$age= array("12","13","14");
$arrcombine = array_combine($name,$age);
print_r($arrcombine);
?>
</body>
</html>
Output:
Array ( [John] => 12 [Peter] => 13 [Harry] => 14 )
www.ourcreativeinfo.in
PHP – 3
Example:
<html>
<body>
<?php
// Printing array Keys(not Values)
$a=array("Name"=>"Peter","Age"=>"41","Country"=>"USA");
echo " <b> Array Keys Are :</b><br> <br>";
print_r(array_keys($a));
?>
</body>
</html>
Output :
Array Keys Are :
Array ( [0] => Name [1] => Age [2] => Country )
www.ourcreativeinfo.in
PHP – 3
Example
<!DOCTYPE html>
<html>
<body>
<?php
$a=array("Volvo","XC90",array("BMW","Toyota"));
$reverse=array_reverse($a);echo"<br>";
$preserve=array_reverse($a,true);echo"<br>";
print_r($a); echo"<br>";
print_r($reverse);echo"<br>";
print_r($preserve);echo"<br>";
www.ourcreativeinfo.in
PHP – 3
?>
</body>
</html>
Output
Array ( [0] => Volvo [1] => XC90 [2] => Array ( [0] => BMW [1] => Toyota ) )
Array ( [0] => Array ( [0] => BMW [1] => Toyota ) [1] => XC90 [2] => Volvo )
Array ( [2] => Array ( [0] => BMW [1] => Toyota ) [1] => XC90 [0] => Volvo )
Example
<html>
<body>
<?php
echo "<b><u>Array_change_key_case() Function</b></u> <br><br> ";
$age=array("PETER"=>"35","BEN"=>"37","JOE"=>"43");
$student=array("name"=>"ram","address"=>"basav circle","city"=>"chikodi", "country"=>
"Indian");
echo "<b>1) Array_change_key_case() Function CASE_LOWER() </b><br><br> ";
print_r(array_change_key_case($age,CASE_LOWER));
www.ourcreativeinfo.in
PHP – 3
echo"<br><br><br><br>";
echo "<b>2) Array_change_key_case() Function CASE_UPPER()</b> <br><br> ";
print_r(array_change_key_case($student,CASE_UPPER));
?>
</body>
</html>
Output
Array_change_key_case() Function
1) Array_change_key_case() Function CASE_LOWER()
Array ( [peter] => 35 [ben] => 37 [joe] => 43 )
2) Array_change_key_case() Function CASE_UPPER()
Array ( [NAME] => ram [ADDRESS] => basav circle [CITY] => chikodi [COUNTRY] => Indian )
6. PHP array_column() Function:
The array_column() function returns the values from a single column in the input array.
Example:
<html>
<body>
<?php
echo "<b><u><center><h2>Array_column() Function</h2></center></b></u> <br><br> ";
$a = array(
array(
'id' => 5698,
'first_name' => 'Peter',
www.ourcreativeinfo.in
PHP – 3
www.ourcreativeinfo.in
PHP – 3
Example:
<html>
<body>
<?php
echo "<b><center><h2>Array_diff() Function on 2 Arrays</h2></center></b><br><br> ";
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"white");
$result=array_diff($a1,$a2);
print_r($result);
?> </body> </html>
Output :
Array_diff() Function on 2 Arrays
Array ( [c] => blue [d] => yellow )
8. PHP array_push() Function:
The array_push() function inserts one or more elements to the end of an array.
Example :
www.ourcreativeinfo.in
PHP – 3
<html>
<body>
<?php
echo "<b><center><h2>Array_push() Function </h2></center></b><br><br> ";
$a=array("red","green");
array_push($a,"blue","yellow","pink");
print_r($a);
?>
</body>
</html>
Output:
Array_push() Function
Array ( [0] => red [1] => green [2] => blue [3] => yellow [4] => pink )
9. PHP array_pop() Function:
Note : Returns the last value of array. If array is empty, or is not an array, NULL will be returned.
Example:
<html>
<body>
<?php
echo "<b><center><h2>Array_pop() Function </h2></center></b><br><br> ";
$a=array("red","green","blue","pink","white");
array_pop($a);
print_r($a);
echo"<br>";
array_pop($a);
print_r($a); ?></body></html>
Output :
Array_pop() Function
Array ( [0] => red [1] => green [2] => blue [3] => pink )
www.ourcreativeinfo.in
PHP – 3
Array ( [0] => red [1] => green [2] => blue )
Example
<html>
<body>
<?php
echo "<b><center><h2>Array_product() Function </h2></center></b><br><br> ";
$a=array(5,5);
echo"Product of array(5,5) = ".(array_product($a));
echo"<br>";
$a=array(5,5,2);
echo"Product of array(5,5,2) = ".(array_product($a));
echo"<br>";
$a=array(5,2,1,3);
echo"Product of array(5,2,1,3) = ".(array_product($a));
echo"<br>";
$a=array(25,72,91,0);
echo"Product of array(25,72,91,0) = ".(array_product($a));
echo"<br>";
$a=array(1.1,2.2,3.4);
echo"Product of array(1.1,2.2,3.4) = ".(array_product($a));
echo"<br>";
?>
</body>
</html>
Output
www.ourcreativeinfo.in
PHP – 3
Array_product() Function
Product of array(5,5) = 25
Product of array(5,5,2) = 50
Product of array(5,2,1,3) = 30
Product of array(25,72,91,0) = 0
Product of array(1.1,2.2,3.4) = 8.228
Example:
<html>
<body>
<?php
echo "<b><center><h2>Array_sum() Function </h2></center></b><br><br> ";
$a=array(5,5);
echo"Sum of array(5,5) = ".(array_sum($a));
echo"<br>";
$a=array(5,5,2);
echo"Sum of array(5,5,2) = ".(array_sum($a));
echo"<br>";
$a=array(5,2,1,3);
echo"Sum of array(5,2,1,3) = ".(array_sum($a));
echo"<br>";
$a=array(25,72,91,0);
echo"Sum of array(25,72,91,0) = ".(array_sum($a));
echo"<br>";
$a=array(1.1,2.2,3.4);
www.ourcreativeinfo.in
PHP – 3
Example
<html>
<body>
<?php
echo "<b><center><h2>in_array() Function </h2></center></b><br><br> ";
$people = array("Peter", "Joe", "Glenn", "Cleveland");
if (in_array("Glenn", $people))
{
echo "Match found";
}
www.ourcreativeinfo.in
PHP – 3
else
{
echo "Match not found";
}
?> </body> </html>
Output :
in_array() Function
Match found
Example:
<html>
<body>
<?php
echo "<b><center><h2>Array_replace () Function </h2></center></b><br><br> ";
$a1=array("red","green");
$a2=array("blue","yellow");
echo"Replaced Array Values are :<br> ";
print_r(array_replace($a1,$a2));
echo"<br><br>";
$a1=array("a"=>"red","b"=>"green","c"=>"pink");
$a2=array("a"=>"orange","burgundy","yellow");
echo"Replaced Array Values are :<br> ";
www.ourcreativeinfo.in
PHP – 3
print_r(array_replace($a1,$a2));
echo"<br><br>";
$a1=array("red","green");
$a2=array("blue","yellow");
$a3=array("orange","burgundy");
echo"Replaced Array Values are :<br> ";
print_r(array_replace($a1,$a2,$a3));
?></body></html>
Output :
Array_replace () Function
Replaced Array Values are :
Array ( [0] => blue [1] => yellow )
Replaced Array Values are :
Array ( [a] => orange [b] => green [c] => pink [0] => burgundy [1] => yellow )
Replaced Array Values are :
Array ( [0] => orange [1] => burgundy )
Get a Date:
The required format parameter of the date() function specifies how to format the date (or
time).
Here are some characters that are commonly used for dates:
d - Represents the day of the month (01 to 31)
m - Represents a month (01 to 12)
www.ourcreativeinfo.in
PHP – 3
Today is 2020/11/03
Today is 2020.11.03
Today is 2020-11-03
Today is Tuesday
Get a Time:
Here are some characters that are commonly used for times:
H - 24-hour format of an hour (00 to 23)
h - 12-hour format of an hour with leading zeros (01 to 12)
i - Minutes with leading zeros (00 to 59)
s - Seconds with leading zeros (00 to 59)
a - Lowercase Ante meridiem and Post meridiem (am or pm)
The example below outputs the current time in the specified format:
<html>
<body>
<?php
www.ourcreativeinfo.in
PHP – 3
OUTPUT:
The time is 07:25:37am
Important Questions:
2 Marks
1. What is Associative Array? Give simple example.
2. Which function is used to get length of an array. Give simple example.
3. What are PHP string functions? List any 5.
4. With an example explain floor and ceil.
5. Write a PHP program to add and remove Array elements.
6. Write a PHP program for checking Date validity.
5 and 10 Marks
1. List and explain different Types of Arrays in PHP.
2. Explain arrays with forms with an Example.
3. Explain multidimensional arrays in PHP with example
4. List and explain different characters that are used to specify date and time.
5. Define PHP arrays. Write Syntax to create PHP arrays and give simple Example.
6. Explain PHP numeric functions with example.
7. List and explain Different Sorting functions that are present in PHP.
www.ourcreativeinfo.in