0% found this document useful (0 votes)
33 views71 pages

arrays unit 3

The document provides an overview of arrays in PHP, including how to create, manipulate, and traverse them. It covers different types of arrays such as associative and multidimensional arrays, as well as various PHP array functions for sorting, merging, and extracting elements. Additionally, it includes practical exercises and questions to reinforce understanding of array concepts.

Uploaded by

NIKLAUS
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)
33 views71 pages

arrays unit 3

The document provides an overview of arrays in PHP, including how to create, manipulate, and traverse them. It covers different types of arrays such as associative and multidimensional arrays, as well as various PHP array functions for sorting, merging, and extracting elements. Additionally, it includes practical exercises and questions to reinforce understanding of array concepts.

Uploaded by

NIKLAUS
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/ 71

ARRAYS

An array stores multiple values in one single variable:


<?php
$colors=array("red","yellow","blue","green");
echo $colors[0]."<br>";
echo $colors[1];
echo $colors[2];
echo $colors[3];
?>
<?php
$colors=["red","yellow","blue","green"];
for($i=0;$i<4;$i++)
echo $colors[$i];
?>
An array stores multiple values in one single variable:
TRY THIS

• $color = array('white', 'green', 'red'')


Write a PHP script which will display the colors in the following way :

Output :
white, green, red,

• $color = array('white', 'green', 'red', 'blue', 'black');

Write a script which will display the following string

"The memory of that scene for me is like a frame of film forever frozen at that
moment: the red carpet, the green lawn, the white house, the leaden sky. The new
president and his first lady. - Richard M. Nixon"
and the words 'red', 'green' and 'white' will come from $color.
Associative Array
<?php
$age=array("bill"=>25, "john"=>28, "deep"=>27);
var_dump($age); Shows the data types of values
echo $age["john"]."<br>";
echo $age["bill"];
echo $age["deep"];
?>
TRY THIS

Create an associative array representing a person's information,


including name, age, and email.

Create an associative array of fruits and their corresponding colors. Loop


through the array and print out each fruit and its color.
Loop Through an Associative Array

To loop through and print all the values of an associative array, you could use a foreach loop, like this:

<?php
$age
= array("Peter"=>"35", "Ben"=>"37", "Joe
"=>"43");

foreach($age as $x => $x_value) {


echo "Key=" . $x . ", Value=" .
$x_value;
echo "<br>";
}
?> Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43
Multidimensional Arrays
◦ Multidimensional array as an array of arrays
◦ Every element in the array holds a sub-array within it.
Var dump in php
Print_r
Output
Unset()

If you want to delete the whole array you need to use unset($arr);
Sort Array in Ascending Order - sort()

The following example sorts the elements of the $cars array in ascending alphabetical order:
Sort Array in Descending Order - rsort()

Sort Array (Ascending Order), According to Value - asort()


Sort Array (Ascending Order), According to Key - ksort()

Sort Array (Descending Order), According to Value - arsort()


TRY THIS

Write a PHP script to sort the following associative array :


array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40") in

a) ascending order sort by value


b) ascending order sort by Key
c) descending order sorting by Value
d) descending order sorting by Key
PHP Array Functions
The array functions allow you to access and manipulate arrays.
PHP Array Functions
PHP Array Functions

PHP array_chunk() Function

The array_chunk() function splits an array into chunks of new arrays.


PHP Array Functions
PHP array_merge() Function

The array_merge() function merges one or more arrays into one array.
Traversing Arrays
Traverse an array means to access each element (item) stored in
the array so that the data can be checked or used as part of a
process.

To visit each and every element of an array is known as


traversing array
Which PHP function is used to add
elements to the end of an array?
A) array_push()
B) array_pop()
C) array_shift()
D) array_unshift()
Which PHP function is used to add
elements to the end of an array?
A) array_push()
B) array_pop()
C) array_shift()
D) array_unshift()
Which PHP function is used to combine two or more arrays into a single array?

A) array_merge()
B) array_combine()
C) array_union()
D) array_join()
Which PHP function is used to combine two or more arrays into a single array?

A) array_merge()
B) array_combine()
C) array_union()
D) array_join()
Which function is used to find the number of elements in an array in PHP?

A) count()
B) length()
C) size()
D) sizeof()
Which function is used to find the number of elements in an array in PHP?

A) count()
B) length()
C) size()
D) sizeof()
In PHP, how do you check if a specific element exists in an array?

A) element_exists()
B) in_array()
C) array_search()
D) find_element()
In PHP, how do you check if a specific element exists in an array?

A) element_exists()
B) in_array()
C) array_search()
D) find_element()
Which method is used to remove an element
from the beginning of an array in PHP?

A) array_pop()
B) array_shift()
C) array_slice()
D) array_splice()
Which method is used to remove an element
from the beginning of an array in PHP?

A) array_pop()
B) array_shift()
C) array_slice()
D) array_splice()
Which PHP function is used to remove duplicate values from an array?

A) array_unique()
B) array_distinct()
C) array_remove_duplicates()
D) array_filter()
Which PHP function is used to remove duplicate values from an array?

A) array_unique()
B) array_distinct()
C) array_remove_duplicates()
D) array_filter()
What does the array_keys() function in PHP return?

A) The values of an array


B) The keys of an array
C) The combined keys and values of an array
D) The sorted keys of an array
What does the array_keys() function in PHP return?

A) The values of an array


B) The keys of an array
C) The combined keys and values of an array
D) The sorted keys of an array
Which array function is used to extract a portion of an array?

A) array_extract()
B) array_slice()
C) array_subarray()
D) array_part()
Which array function is used to extract a portion of an array?

A) array_extract()
B) array_slice()
C) array_subarray()
D) array_part()
5. Which in-built function will add a value to the end
of an array?

A. array_unshift()
B. into_array()
C. inend_array()
D. array_push()
A. array_unshift()
B. into_array()
C. inend_array()
D. array_push()
4. Which of the following are correct
ways of creating an array?
i) arr[0] = "letsfindcourse";
ii) $arr[] = array("letsfindcourse");
iii) $arr[0] = "letsfindcourse";
iv) $arr = array("letsfindcourse");
i) arr[0] = "letsfindcourse";
ii) $arr[] = array("letsfindcourse");
iii) $arr[0] = "letsfindcourse";
iv) $arr = array("letsfindcourse");
6. Which function returns an array consisting
of associative key/value pairs?
A. count()
B. array_count()
C. array_count_values()
D. count_values()
Ans : C
Explanation: The function array_count_values() will count all the values of an
array.
8. As compared to associative arrays
vector arrays are much
A. Faster
B. Slower
C. Stable
D. None of the above
Ans : A
Explanation: As compared to associative arrays vector arrays
are much Faster.
11. Keys in array can be non-
sequential.
A. True
B. False
C. Depend on program
D. Keys does not exist in array
Ans : A
14. What is the use of array_unshift()
function?
A. Function is used to print the array in readable
format.
B. Adds elements to the beginning of the array and
returns the size of array.
C. Function can be used to verify if a variable is an
array. Returns TRUE or FALSE
D. Adds elements to the end of the array and returns
the size of array.
Ans : B
Explanation: array_unshift() Adds elements to
the beginning of the array and returns the size
of array.

You might also like