Web Technology II · Unit 4
Arrays and Data Structures
Exam-focused notes for Arrays and Data Structures (Web Technology II, BIT301): what the TU syllabus asks and how it has actually been tested, with 6 solved past questions from this unit.
What this unit covers
- Indexed arrays and associative arrays
- Array creation and manipulation
- Array slicing with array_slice function
- Array element insertion and removal with array_splice
- Conversion between arrays and variables
- Extract and compact functions
- String arrays and storage
Indexed arrays and associative arrays
How are indexed arrays different from associative arrays? Write a PHP program to demonstrate the conversion between array and variables.[10]
Feature Indexed Array Associative Array --------- Index Type Uses numeric indices (0, 1, 2, ...) Uses named string keys Key Assignment Keys are assigned automatically Keys are assigned manually by the programmer Access Method Elements accessed by position n...
Full solved answer →Conversion between arrays and variables
How conversion between arrays and variables is done using extract() and compact() functions? [5]
--- The extract() function converts an associative array into individual variables. Each key of the array becomes a variable name, and the corresponding value becomes the variable's value. How it works: - Key "name" → variable $name = "Ram" - Key "age" → va...
Full solved answer →How conversion between array and variables is done? [5]
An array is a single variable that holds many values, so a PHP script constantly moves data in both directions: unpacking the elements of an array into separate scalar variables that can be used one at a time, and gathering separate variables back into one ...
Full solved answer →Array element insertion and removal with array_splice
How can you remove and insert elements in an array? Explain by writing a PHP program using array_splice() for inserting and removing elements in an array. [5]
arraysplice() is a built-in PHP function used to remove, replace, or insert elements in an array. It modifies the original array directly. --- Parameter Description ------ $array The input array (modified in place) $offset Starting index position $length Nu...
Full solved answer →Extract and compact functions
How can you store string type data in an array? Write a PHP program to illustrate use of extract and compact functions for conversion between arrays and variables.[10]
--- In PHP, strings can be stored in arrays just like any other data type. PHP supports both indexed arrays and associative arrays for storing string data. - Each string is stored at a numeric index starting from 0. - $fruits[0] gives "Apple", $fruits[1] gi...
Full solved answer →Array slicing with array_slice function
Write a PHP program to extract subset of a string array using array_slice() function. [5]
arrayslice() is a built-in PHP function that extracts a portion (subset) of an array and returns it as a new array. Syntax: Parameter Description ------ array The input array start Starting index (0-based) length Number of elements to extract (optional) pre...
Full solved answer →Make Unit 4 stick
Practice BIT301 with flashcards & quizzes